GitHub 上的 googleads/googleads-shopping-samples 存放區包含每個用戶端程式庫的常見作業程式碼範例。舉例來說,googleads-shopping-samples/python/shopping/content/products/ 中的範例提供使用 products
資源搭配 Python 進行常見作業的程式碼。在本指南中,您會從一個空白檔案開始,並建立一個插入新產品的範例,以便瞭解與 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
如果呼叫成功,服務會將下列訊息輸出至終端機:Product withOfferId "offerId" is created.
如要確認產品是否已成功新增,請使用
products.list
方法的 API Explorer 來傳回 Merchant Center 帳戶中的所有產品。在
products.list
方法的 API Explorer 中輸入下列的值:- 輸入您的
merchantId
。
- 在「憑證」部分中,選取 [Google OAuth 2.0] 和 [API 金鑰]。
- 按一下 [Execute] (執行) 按鈕。
- 如果出現系統提示,請使用與 Merchant Center 帳戶相關聯的 Google 帳戶登入。
成功新增產品後,產品資料會出現在 API Explorer 回應中。
- 輸入您的
商家必須負責遵守購物廣告和免費產品資訊政策。一旦發現違反這些政策的內容或行為,Google 購物有權實施這些政策並採取適當措施。