發出 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 withofferId「offerId」已建立。

  6. 如要確認產品是否已成功新增,請使用 products.list 方法的 API Explorer 傳回 Merchant Center 帳戶中的所有產品。

    products.list 方法的 API Explorer 中輸入下列值:

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

    如果產品新增成功,產品資料就會顯示在 API Explorer 回應中。

商家必須遵守購物廣告免費產品資訊政策。Google 購物有權強制執行這些政策,並在發現違反這些政策的內容或行為時採取適當回應。