เรียก API

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

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

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

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

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

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