Make 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.

    At the beginning of my-insert.py, add the following code:

    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.

    At the end of my-insert.py, add the following code:

    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.

    At the end of my-insert.py, add the following code:

    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/python/ and run:

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

    If the call was successful, the service prints the following message to the terminal: 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 for the products.list method, enter the following values:

    1. Enter your merchantId.
    1. In the Credentials section, select Google OAuth 2.0 and API key.
    2. Click the Execute button.
    3. 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.

Merchants are responsible for complying with the Shopping ads and free listings policies. Google Shopping reserves the right to enforce these policies and respond appropriately if we find content or behavior that violates these policies.