發出 API 呼叫

GitHub 上的 googleads/googleads-shopping-samples 存放區包含各個用戶端程式庫的常用操作範例程式碼。舉例來說,googleads-shopping-samples/python/shopping/content/products/ 中的範例會提供使用 Python 搭配 products 資源執行常見作業的程式碼。在本指南中,您將從空白檔案開始,並建立可插入新產品的範例,以便查看與 Content API 整合的應用程式的基本結構和必要元件。最終結果會類似 products/insert.py 範例檔案中的範例。接著,您可以使用 API Explorer 針對 products.list 方法驗證產品是否已成功新增。

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

  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
    

    如果呼叫成功,服務會在終端機上顯示以下訊息: 已建立 offerId 為「offerId」的產品。

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

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

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

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

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