เรียก 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 ขอสงวนสิทธิ์ในการใช้นโยบายเหล่านี้และดำเนินการตามความเหมาะสมหากพบเนื้อหาหรือพฤติกรรมที่ละเมิดนโยบายเหล่านี้