googleads/googleads-shopping-samples
GitHub 上的 Repository 包含各用戶端常見作業的程式碼範例
資源庫。舉例來說
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
如果呼叫成功,服務會在終端機輸出下列訊息: 優惠 ID 為「offerId」的產品。
如要確認產品是否已成功新增,請使用 API Explorer 使用
products.list
方法,傳回 Merchant Center 中的所有產品 讓他們使用服務帳戶在 適用於
products.list
方法的 APIs Explorer 輸入下列值:- 輸入
merchantId
。
- 在「Credentials」(憑證) 部分中,選取「Google OAuth 2.0」(Google OAuth 2.0) 和「API key」(API 金鑰)。
- 按一下 [Execute] (執行) 按鈕。
- 如果系統顯示提示,請使用與商家相關聯的 Google 帳戶登入 。
如果產品新增成功,產品資料就會顯示在 API 中。 回應。
- 輸入
商家必須遵守 廣告和免費 產品資訊政策。Google 敬上 Google 購物有權強制執行這些政策及採取適當的回應 如果發現違反這些政策的內容或行為。