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
呼び出しが成功すると、サービスは次のメッセージをターミナルに出力します。 Product with offerId "offerId" was created.
商品が正常に追加されたことを確認するには、API Explorer の
products.list
メソッドを使用して、Merchant Center アカウント内のすべての商品を返します。products.list
メソッドの API Explorer で、次の値を入力します。merchantId
を入力します。
- [認証情報] セクションで、[Google OAuth 2.0] と [API キー] を選択します。
- [Execute] ボタンをクリックします。
- ログインを求められたら、Merchant Center アカウントに関連付けられている Google アカウントでログインします。
商品が正常に追加されると、API エクスプローラ レスポンスに商品データが表示されます。
販売者は、ショッピング広告と無料リスティングのポリシーを遵守する責任を負います。Google ショッピングは、これらのポリシーを適用し、ポリシーに違反するコンテンツや行為が認められた場合は適切な措置をとる権利を有します。