GitHub の googleads/googleads-shopping-samples リポジトリには、各クライアント ライブラリの一般的なオペレーションのサンプルコードが含まれています。たとえば、googleads-shopping-samples/python/shopping/content/products/ のサンプルでは、Python で products
リソースを使用し、一般的なオペレーション用のコードを提供しています。このガイドでは、空のファイルから始め、新しい商品を挿入するサンプルを作成します。これにより、Content API と統合するアプリケーションの基本構造と必要なコンポーネントを確認できます。最終的な結果は、products/insert.py サンプル ファイル内の例と同様になります。その後、products.list
メソッドに API Explorer を使用して、プロダクトが正常に追加されたことを確認できます。
最初の通話を発信する手順は次のとおりです。
googleads-shopping-samples/python/shopping/content/products/ ディレクトリに空の my-insert.py ファイルを作成します。このファイルに、以下の手順のすべてのコードを追加します。
必要なモジュールの 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
一意のプロダクト 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' } }
コマンドラインからスクリプトを実行したときに実行する関数を作成する。この関数は、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)
スクリプトを実行して API 呼び出しを実行するには、ターミナル ウィンドウから googleads-shopping-samples/python/ に移動して次のコマンドを実行します。
python -m shopping.content.products.my-insert
呼び出しが成功した場合、サービスは「offerId が offerId である商品が作成されました。」というメッセージをターミナルに出力します。
商品が正常に追加されたことを確認するには、
products.list
メソッドの API Explorer を使用して Merchant Center アカウントのすべての商品を返します。products.list
メソッドの API Explorer で、次の値を入力します。merchantId
を入力します。
- [認証情報] セクションで、[Google OAuth 2.0] と [API キー] を選択します。
- [Execute] ボタンをクリックします。
- プロンプトが表示されたら、Merchant Center アカウントに関連付けられている Google アカウントでログインします。
商品が正常に追加されると、API Explorer のレスポンスに商品データが表示されます。
販売者は、ショッピング広告と無料リスティングに関するポリシーを遵守する必要があります。Google ショッピングは、これらのポリシーを適用し、このポリシーに違反するコンテンツや行為が認められた場合、適切な措置を取る権利を有します。