ביצוע קריאה ל-API

האפשרות googleads/googleads-shopping-samples המאגר ב-GitHub מכיל קוד לדוגמה לפעולות נפוצות של כל לקוח לספרייה. לדוגמה, הדוגמאות googleads-shopping-samples/python/shopping/content/products/ מספקים קוד לפעולות נפוצות באמצעות products ב-Python. במדריך הזה מתחילים עם קובץ ריק דוגמה שמכניסה מוצר חדש, כדי שאפשר יהיה לראות את את המבנה והרכיבים הנדרשים של אפליקציות שמשתלבות עם Content API. התוצאה הסופית תהיה דומה לדוגמה קובץ לדוגמה products/insert.py. לאחר מכן אפשר להשתמש API Explorer ל-method products.list כדי לוודא שהמוצר נוסף בהצלחה.

כדי לבצע את השיחה הראשונה, מבצעים את השלבים הבאים:

  1. ב-googleads-shopping-samples/python/shopping/content/products/ יוצרים קובץ my-insert.py ריק. הוסיפו את כל הקוד את השלבים הבאים לקובץ הזה.

  2. מוסיפים את הצהרות הייבוא למודולים הנדרשים.

    בתחילת הקטע 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
    
  3. יש להגדיר מזהה מוצר ייחודי וליצור מילון יחד עם המוצר להגדרה.

    בסוף הקטע 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'
         }
    }
    
  4. ליצור פונקציה שרצה כשהסקריפט מופעל משורת הפקודה. בונה אובייקט שירות כדי לקיים אינטראקציה עם ה-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)
    
    
  5. כדי להריץ את הסקריפט ולהפעיל את הקריאה ל-API, צריך לעבור מחלון טרמינל googleads-shopping-samples/python/ ומריצים:

    python -m shopping.content.products.my-insert
    

    אם השיחה בוצעה בהצלחה, השירות ידפיס את ההודעה הבאה במסוף: מוצר עם מזהה המבצע "offerId" נוצרה.

  6. כדי לוודא שהמוצר נוסף בהצלחה, משתמשים ב-API Explorer עבור שיטה products.list להחזרת כל המוצרים ב-Merchant Center חשבון.

    ב API Explorer ל-method products.list, מזינים את הערכים הבאים:

    1. מזינים את merchantId.
    1. בקטע פרטי כניסה, בוחרים באפשרות Google OAuth 2.0 ומפתח API.
    2. לוחצים על הלחצן Execute.
    3. אם מתבקשים, נכנסים לחשבון Google שמשויך לחשבון המוכר Center.

    אם המוצר נוסף בהצלחה, נתוני המוצר יופיעו ב-API. את תגובת החוקרים.

המוכרים אחראים לציית להנחיות שופינג פרסומות ובחינם המדיניות בנושא כרטיסי מוצר. Google צוות Google שופינג שומר לעצמו את הזכות לאכוף את כללי המדיניות האלה ולהגיב בהתאם. אם נמצא תוכן או התנהגות שמפרים את המדיניות הזו.