API 呼び出しを行う

GitHub の googleads/googleads-shopping-samples リポジトリには、各クライアント ライブラリの一般的なオペレーションのサンプルコードが含まれています。たとえば、googleads-shopping-samples/python/shopping/content/products/ のサンプルには、Python で products リソースを使用する一般的なオペレーションのコードが含まれています。このガイドでは、まず空のファイルを作成し、新しいアイテムを挿入するサンプルを作成します。これにより、Content API と統合するアプリの基本的な構造と必須コンポーネントを確認できます。最終的な結果は、products/insert.py サンプル ファイルの例のようになります。その後、API Explorerproducts.list メソッドを使用すると、商品が正常に追加されたことを確認できます。

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

  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. 商品が正常に追加されたことを確認するには、API Explorer で products.list メソッドを使用し、Merchant Center アカウント内のすべての商品を返します。

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

    1. merchantId を入力してください。
    1. [Credentials] で [Google OAuth 2.0] と [API key] を選択します。
    2. [Execute] ボタンをクリックします。
    3. プロンプトが表示されたら、Merchant Center アカウントに関連付けられている Google アカウントでログインします。

    商品が正常に追加されると、API Explorer のレスポンスに商品データが表示されます。

販売者は、ショッピング広告無料リスティングに関するポリシーに準拠する必要があります。Google ショッピングは、こうしたポリシーを適用し、ポリシーに違反するコンテンツや行為が認められた場合に適切に対応する権限を有します。