Making an API Call

The googleads/googleads-shopping-samples repo on GitHub contains sample code for common operations for each client library. For example, the samples in googleads-shopping-samples/python/shopping/content/products/ provide code for common operations using the products resource with Python. In this guide, you start with an empty file and build up an example that inserts a new product, so that you can see the basic structure and required components of applications that integrate with the Content API. The end result will be similar to the example in the products/insert.py sample file. You can then use the API Explorer for the products.list method to verify that the product was added successfully.

To make your first call, complete the following steps:

  1. In the googleads-shopping-samples/python/shopping/content/products/ directory, create an empty my-insert.py file. Add all of the code in the following steps to this file.

  2. Add the import statements for the required modules:

    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. Define a unique product ID and create a dictionary with the product definition:

    ...
    
    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. Create a function that runs when the script is run from the command line. The function constructs a service object to interact with the Content API, gets the merchant ID from the configuration file, constructs the request, and executes the request to make the API call:

    ...
    
    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. To run the script and execute the API call, from a terminal window, navigate to googleads-shopping-samples-master/python/ and run:

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

    If the call was successful, the following message appears: Product with offerId "offerId" was created.

  6. To verify that the product was added successfully, use the API Explorer for the products.list method to return all of the products in your Merchant Center account.

    In the API Explorer, enter the following values:

    1. Enter your merchantId.
    2. In the Credentials section, select Google OAuth 2.0 and API key.
    3. Click the Execute button.
    4. If prompted, sign in with the Google account associated with your Merchant Center account.

    If the product was added successfully, the product data appears in the API explorer response.