เรียก API

ที่เก็บ googleads/googleads-shopping-ตัวอย่าง ใน GitHub มีโค้ดตัวอย่างสำหรับการดำเนินการทั่วไปสำหรับ ไลบรารีของไคลเอ็นต์แต่ละรายการ ตัวอย่างเช่น ตัวอย่างใน googleads-shopping-samples/python/shopping/content/products/ มีโค้ด สำหรับการดำเนินการทั่วไปโดยใช้ทรัพยากร products กับ Python ในคู่มือนี้ คุณจะเริ่มต้นด้วยไฟล์เปล่าและสร้างตัวอย่างที่แทรกผลิตภัณฑ์ใหม่ เพื่อให้คุณเห็นโครงสร้างพื้นฐานและคอมโพเนนต์ที่จำเป็นของแอปพลิเคชันซึ่งผสานรวมกับ Content API ผลลัพธ์สุดท้ายจะคล้ายกับตัวอย่างในไฟล์ตัวอย่าง products/insert.py จากนั้นจึงใช้โปรแกรมสำรวจ API สำหรับเมธอด products.list เพื่อยืนยันว่าเพิ่มผลิตภัณฑ์สำเร็จแล้ว

หากต้องการโทรครั้งแรก ให้ทำตามขั้นตอนต่อไปนี้

  1. ในไดเรกทอรี googleads-shopping-samples/python/shopping/content/products/ ให้สร้างไฟล์ googleads-shopping-samples/python/shopping/content/products/ ที่ว่างเปล่า เพิ่มโค้ดทั้งหมดใน ขั้นตอนต่อไปนี้ลงในไฟล์นี้

  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. กำหนด 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'
         }
    }
    
  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
    

    หากเรียกใช้สำเร็จ บริการจะพิมพ์ข้อความต่อไปนี้ไปยังเทอร์มินัล: สร้างผลิตภัณฑ์ที่มี Offerwall "offerId" แล้ว

  6. หากต้องการยืนยันว่าเพิ่มผลิตภัณฑ์เรียบร้อยแล้ว ให้ใช้โปรแกรมสำรวจ API สำหรับเมธอด products.list เพื่อส่งคืนผลิตภัณฑ์ทั้งหมดในบัญชี Merchant Center

    ใน API Explorer สำหรับเมธอด products.list ให้ป้อนค่าต่อไปนี้

    1. ป้อนmerchantId
    1. ในส่วนข้อมูลเข้าสู่ระบบ ให้เลือก Google OAuth 2.0 และคีย์ API
    2. คลิกปุ่มดำเนินการ
    3. หากได้รับข้อความแจ้ง ให้ลงชื่อเข้าใช้ด้วยบัญชี Google ที่เชื่อมโยงกับบัญชี Merchant Center

    หากเพิ่มผลิตภัณฑ์สำเร็จ ข้อมูลผลิตภัณฑ์จะปรากฏในการตอบกลับของโปรแกรมสำรวจ API

ผู้ขายมีหน้าที่รับผิดชอบในการปฏิบัติตามนโยบายโฆษณา Shopping และข้อมูลที่แสดงฟรี Google Shopping ขอสงวนสิทธิ์ในการบังคับใช้นโยบายเหล่านี้และตอบสนองอย่างเหมาะสมหากเราพบเนื้อหาหรือพฤติกรรมที่ละเมิดนโยบายเหล่านี้