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