API 呼び出しを行う

googleads/googleads-shopping-samples GitHub のリポジトリには、各クライアントの一般的なオペレーションのサンプルコードが含まれています。 ライブラリです。たとえば、先ほど説明したサンプルでは、 googleads-shopping-samples/python/shopping/content/products/ コード 一般的な操作については、 products 作成しました。このガイドでは、空のファイルから始めて、 新しい商品を挿入するサンプルを作成して、基本的な アプリケーションに必要なコンポーネントの構造と必要なコンポーネントを Content API です。最終的な結果は products/insert.py サンプル ファイル。その後、 API Explorer products.list メソッドの 0.0.0.0 を実行して、商品が正常に追加されたことを確認します。

最初の呼び出しを行うには、次の手順を行います。

  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 を使用して Merchant Center 内のすべての商品を返す products.list メソッド あります。

    products.list メソッドの API Explorer 次の値を入力します。

    1. merchantId を入力してください。
    で確認できます。
    1. [Credentials] で [Google OAuth 2.0] と [API key] を選択します。
    2. [Execute] ボタンをクリックします。
    3. メッセージが表示されたら、販売者に関連付けられた Google アカウントでログインします。 。

    商品が正常に追加されると、商品データが API に表示されます。 表示されます。

販売者は、これを遵守する責任を負います。 広告無料 リスティングポリシーについて説明します。Google Google ショッピングは、これらのポリシーを適用し、適切に対応する権限を有します。 ポリシー違反のコンテンツまたは行為が確認された場合。