进行 API 调用

GitHub 上的 googleads/googleads-shopping-samples 代码库包含每个客户端库常见操作的示例代码。例如,googleads-shopping-samples/python/shopping/content/products/ 中的示例为搭配使用 products 资源和 Python 资源提供了常用操作的代码。在本指南中,您将从一个空文件着手,构建一个示例以插入新产品,以便您了解与 Content API 集成的应用的基本结构和所需组件。最终结果与 products/insert.py 示例文件中的示例类似。然后,您可以使用 products.list 方法的 API Explorer 验证商品是否已成功添加。

首次拨打电话时,请完成以下步骤:

  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
    

    如果调用成功,该服务会将以下消息输出到终端:product with offerId "offerId" was created.

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

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

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

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

商家有责任遵守购物广告政策和非付费商品详情政策。如果发现违反这些政策的内容或行为,Google 购物有权强制执行这些政策并采取适当措施。