进行 API 调用

googleads/googleads-shopping-samples GitHub 上的 repo 包含针对每个客户端执行常见操作的示例代码 库。例如, googleads-shopping-samples/python/shopping/content/products/ 提供代码 使用 products 下载资源在本指南中,您将从一个空文件开始构建 举例说明如何插入新产品 结构和必要的应用组件,这些组件与 Content API。最终结果与 products/insert.py 示例文件。然后,您可以使用 API Explorer 用于 products.list 方法,以验证商品是否已成功添加。

要进行首次调用,请完成以下步骤:

  1. googleads-shopping-samples/python/shopping/content/products/ 目录中,创建一个空的 my-insert.py 文件。将所有代码添加到 对此文件执行下列步骤

  2. 为所需模块添加 import 语句。

    my-insert.py 的开头添加以下代码:

    from __future__ import print_function
    import sys
    
    # The common module provides setup functionality used by the samples,
    # such as authentication and unique id generation.
    from shopping.content import common
    
  3. 定义唯一商品 ID 并创建包含相应商品的字典 定义。

    my-insert.py 的末尾,添加以下代码:

    offer_id = 'book#%s' % common.get_unique_id()
    product = {
         'offerId':
             offer_id,
         'title':
             'A Tale of Two Cities',
         'description':
             'A classic novel about the French Revolution',
         'link':
             'http://my-book-shop.com/tale-of-two-cities.html',
         'imageLink':
             'http://my-book-shop.com/tale-of-two-cities.jpg',
         'contentLanguage':
             'en',
         'targetCountry':
             'US',
         'channel':
             'online',
         'availability':
             'in stock',
         'condition':
             'new',
         'googleProductCategory':
             'Media > Books',
         'gtin':
             '9780007350896',
         'price': {
             'value': '2.50',
             'currency': 'USD'
         },
         'shipping': [{
             'country': 'US',
             'service': 'Standard shipping',
             'price': {
                 'value': '0.99',
                 'currency': 'USD'
             }
         }],
         'shippingWeight': {
             'value': '200',
             'unit': 'grams'
         }
    }
    
  4. 创建一个从命令行运行脚本时运行的函数。通过 函数构造一个与 Content API 交互的服务对象, 从配置文件中获取商家 ID、构建请求,并执行 进行 API 调用的请求

    my-insert.py 的末尾,添加以下代码:

    def main(argv):
      # Construct the service object to interact with the Content API.
      service, config, _ = common.init(argv, __doc__)
    
      # Get the merchant ID from merchant-info.json.
      merchant_id = config['merchantId']
    
      # Create the request with the merchant ID and product object.
      request = service.products().insert(merchantId=merchant_id, body=product)
    
      # Execute the request and print the result.
      result = request.execute()
      print('Product with offerId "%s" was created.' % (result['offerId']))
    
    # Allow the function to be called with arguments passed from the command line.
    if __name__ == '__main__':
      main(sys.argv)
    
  5. 要运行脚本并执行 API 调用,请从终端窗口中转到 添加到 googleads-shopping-samples/python/,然后运行以下命令:

    python -m shopping.content.products.my-insert
    

    如果调用成功,服务会向终端输出以下消息: 优惠 ID 为“offerId”的产品已创建。

  6. 要验证商品是否已成功添加,请使用 API Explorer products.list 方法,用于返回 Merchant Center 中的所有商品 。

    适用于 products.list 方法的 API Explorer 输入以下值:

    1. 输入您的merchantId
    1. 凭据部分中,选择 Google OAuth 2.0API 密钥
    2. 点击执行按钮。
    3. 如果系统提示,请使用与您的 Merchant Center 账号关联的 Google 账号登录 中心账号。

    如果商品添加成功,则商品数据会显示在 API 中 Explorer 响应。

商家有责任遵守购物广告 广告免费 商品详情政策。Google Google 购物保留强制执行这些政策并做出适当回应的权利 如果我们发现了违反这些政策的内容或行为,