Set up your Merchant Center Account
- Create a Merchant Center Account.
- Verify and claim your website URL.
- Configure your Tax & Shipping settings.
- Be sure to remember which email you used to create this merchant center account, as you will need that later. In the screenshots, you'll see it represented as “firstname.lastname@example.org”.
Make your first API request (without a line of code!)
- Head over to the APIs Explorer in our developer documentation for a products list.
Ensure that the logged in Google account is in the 'Users' list in the settings of your Merchant Center account. This is illustrated below:
In the APIs Explorer, toggle the button to authorize requests using OAuth 2.0:
Click Accept to authorize a Google-created API Console Project to temporarily access your Merchant Center account on your behalf to make an API request. Note that this screen should come from google.com, and you should be signed in to your Merchant Center email address.
Enter your Merchant ID from your Merchant Center account into the 'merchantId' field of the form, and click Execute.
- You should see a successful response. Congratulations! You've made your first Shopping API request.
- If you have no products in your Merchant Center yet, there won't be any in the list. But never fear, we'll add some soon. At this point, you can hop to other requests and take a look at what they do. Since this is a RESTful API, all requests work similarly. Good next steps include Products.insert, Inventory.set, and Accountstatuses.get.
Create a Google API Console Project
Requests to the Content API for Shopping are made through your API Console project. You will 'authorize' your API Console project to make requests on your behalf using OAuth2. The username (email address) associated with the Merchant Center account to be accessed must be the same email that accepts the OAuth2 authorization. Let's look at it in more detail:
- Go to the Google API Console.
- From the project drop-down, select an existing project, or create a new one (you can name it anything you like).
Enable the Content API for Shopping service:
- In the list of Google APIs, search for the Content API for Shopping service.
- Select Content API for Shopping from the results list.
- Select Enable API.
- Next, you need to set up the screen Google will show the user when you request
their authorization to access their products. In the left-side menu under "API Manager",
select Credentials, then select the OAuth consent screen tab.
- Choose an Email Address, enter your website/app's name as the Product Name, and select Save.
- Next, set up your OAuth2 client ID.
- In the Credentials tab, select the Create credentials drop-down, then select OAuth client ID.
- Under Application type, choose Other and provide a name. Click Create.
The Oauth client ID and secret appear. Leave this page open for now, as you will need the generated credentials in the next section.
Your first authorized API request using Python
Now we will need to authorize your API Console project to access your Merchant Center account through the Content API for Shopping. This is because the API Console project is acting on your behalf to access your Merchant Center account. Your authorization will allow it to act "as you" in this process. This guide will use Python to demonstrate the API. Other languages are available at our Samples and Libraries page.
Use pip to install the Python client library and the OAuth2 client. More detailed instructions are available here.
$ pip install --upgrade oauth2client $ pip install --upgrade google-api-python-client
Download our Python samples from our GitHub page.
Run the product_list.py file, similar to what we did in the API Sandbox earlier. Enter your merchant ID as a command line argument:
$ python product_list.py <merchant_id>
You will get an error explaining that you must populate the client_secrets.json file. Go ahead and open that up, and refer to the API Manager Credentials page from your API Console project to fill in the Client ID and the Client Secret. Do not share the Client Secret with anyone, as they will be able to access your API Console project with that information!
Now that you have modified your client_secrets.json file, run product_list.py again (with your merchant ID as an argument as before).
$ python product_list.py <merchant_id>
Your terminal will redirect you to your browser to visit a site that starts with https://accounts.google.com/o/oauth2/auth. Ensure that the link opens in a browser that is signed in with the same account that you used to create the Merchant Center account, or one that is in the 'Users' list in Merchant Center settings. This step is analogous to the step in the APIs Explorer when you flipped the toggle to authorize the API Console project to make requests on your behalf. This time, you are doing it with your own project, and you are driving the request with your own code.
Accept the authorization and you should receive a success message.
At this point, the Python script should finish executing, and print out either a list of products that it found, or a message that there were no products in your account. If you get an error such as "User cannot access account 1234567890", it's most likely because you were logged into the wrong account when you authorized the app. From the API's standpoint, the API Console project sent a request on behalf of a user who was not on the list of authorized Merchant Center administrators, so it would, of course, reject that request.
You can experiment with running product_list.py as many times as you want, since it is a read-only request. You can also explore the many other samples that we've created.