เรียก API

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

หรือไม่

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

  1. ในไดเรกทอรี googleads-shopping-sample/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-sample/python/ จากหน้าต่างเทอร์มินัล แล้วเรียกใช้คําสั่งต่อไปนี้

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

    หากโทรสําเร็จ บริการจะพิมพ์ข้อความต่อไปนี้ไปยังเครื่องชําระเงิน สร้างผลิตภัณฑ์ที่มีรหัสข้อเสนอ "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 ขอสงวนสิทธิ์ในการบังคับใช้นโยบายเหล่านี้และตอบกลับอย่างเหมาะสมหากเราพบเนื้อหาหรือพฤติกรรมที่ละเมิดนโยบายเหล่านี้