發出 API 呼叫

GitHub 上的 googleads/googleads-shopping-samples 存放區包含各用戶端程式庫常見作業的程式碼範例。舉例來說,googleads-shopping-samples/python/shopping/content/products/ 中的範例會提供程式碼,說明如何使用 Python 搭配 products 資源執行常見作業。在本指南中,您會從空白檔案開始,逐步建構插入新產品的範例,瞭解與 Content API 整合的應用程式基本結構和必要元件。最終結果會與 products/insert.py 範例檔案中的範例類似。接著,您可以使用 products.list 方法的 API Explorer,確認產品已順利新增。

如要撥打第一通電話,請完成下列步驟:

  1. googleads-shopping-samples/python/shopping/content/products/ 目錄中,建立空白的 my-insert.py 檔案。將下列步驟中的所有程式碼新增至這個檔案。

  2. 新增必要模組的匯入陳述式。

    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 探索工具,傳回 Merchant Center 帳戶中的所有產品。

    在「API Explorer for the products.list method」(products.list 方法的 API Explorer) 中,輸入下列值:

    1. 輸入merchantId
    1. 在「憑證」部分中,選取「Google OAuth 2.0」和「API 金鑰」
    2. 按一下 [Execute] (執行) 按鈕
    3. 如果系統顯示提示,請使用與 Merchant Center 帳戶相關聯的 Google 帳戶登入。

    如果產品新增成功,產品資料會顯示在 API 探索工具的回應中。

商家有責任遵守購物廣告免費產品資訊政策。如果發現有內容或行為違反這些政策,Google 購物保留採取適當處置的權利。