המאגר googleads/googleads-shopping-samples ב-GitHub מכיל קוד לדוגמה של פעולות נפוצות בכל ספריית לקוח. לדוגמה, הדוגמאות שב-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
מגדירים מזהה מוצר ייחודי ויוצרים מילון עם הגדרת המוצר.
בסוף הקובץ 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, מקבלת את מזהה המוכר מקובץ התצורה, יוצרת את הבקשה ומבצעת אותה כדי לבצע את קריאת ה-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.בAPI Explorer של השיטה
products.list
, מזינים את הערכים הבאים:- מזינים את
merchantId
.
- בקטע Credentials בוחרים באפשרות Google OAuth 2.0 ובאפשרות API key.
- לוחצים על הלחצן Execute.
- אם תתבקשו לעשות זאת, נכנסים לחשבון Google שמשויך לחשבון Merchant Center.
אם המוצר נוסף בהצלחה, נתוני המוצר יופיעו בתגובה של API Explorer.
- מזינים את
המוכרים אחראים לציית לכללי המדיניות של מודעות שופינג ושל כרטיסי מוצר חינמיים. ל-Google Shopping שמורה הזכות לאכוף את כללי המדיניות האלה ולפעול בהתאם אם נמצא תוכן או התנהגות שמפירים את כללי המדיניות האלה.