Google Merchant Center & Google Ads ============ # Google Content API for Shopping ## Sandbox Testing Go to Google [API console](https://console.developers.google.com/apis?project=crafty-ensign-255306) and search 'Content API for Shopping'. After selecting the activation button, we can see the page that requires credentail. ![](https://i.imgur.com/8peOtRw.jpg) Push the button 'create credential'. Ensure that the signed-in user is in the user list in [Google Merchant Center](https://merchants.google.com/mc?a=142437616&hl=zh-TW). For example, we will use ```ian.kuo@mile.cloud``` to apply content APIs. ![](https://i.imgur.com/fo6fV2o.png) Enter your Merchant ID from your Merchant Center account into the merchantId field of the form, and click **Execute**. ![](https://i.imgur.com/yfXhTV8.jpg) We should see a successful response. ## Execute the first request in Python In the Google Merchant Center page, select **Settings --- Content API**. Select the **Authentication** tab and click the **+** button to create a new API key. Once the new API Console project has been created and a new **service account** created in that project, the service account ID will be added as a new user to your Merchant Center account. ![](https://i.imgur.com/EUfHK7W.png) In addition, your browser will automatically download a JSON file that contains the **service account key**, named ```content-api-key.json```. If you losing this file, you cannot re-download it, but you can repeat the process above to create a new key. Next, we will run the sample python code to conduct API request. Please follow the steps as follows. + Download our [Python samples](https://github.com/googleads/googleads-shopping-samples/tree/master/python) from our GitHub page. + From within the python directory, use ```pip to install``` needed package dependencies. More detailed instructions are available in the accompanying ```README.md``` files. ```bash= pip install -r requirements.txt ``` + **Creation of configuration directory.** Create the directory ```$(HOME)/shopping-samples``` to store the configuration. + **Merchant Info.** For the Content API for Shopping, take the example ```merchant-info.json``` from the repository root and copy it into ```$(HOME)/shopping-samples/content```. Next, change its contents (```merchantId``` and ```accountSampleUser```) appropriately. Each set of samples uses a different configuration file. ![](https://i.imgur.com/weuzPZF.png) + **Google Application Default Credentials.** Copy the downloaded json file ```content-api-key.json``` for the API key of service account into ```$(HOME)/shopping-samples/content```, and rename it as ```service-account.json```. + Thus, in the directory ```$(HOME)/shopping-samples/content```, we have two files, namely, ```merchant-info.json``` and ```service-account.json```. + Go to the directory ```$(REPOSITORY_ROOT)/python``` and run the ```shopping/content/products/list.py``` file, similar to what we did in the API Sandbox earlier. ```bash= python -m shopping.content.products.list ``` ![](https://i.imgur.com/1sCyd9Q.png) ## API: Products The [page](https://developers.google.com/shopping-content/v2/quickstart?hl=zh-CN) show the usage of the Google Content API for shopping. ### Domain issue Notice that the website url of the priduct shoulb be aligned with the registered domain of the account. Thus, the variable ```website_url``` in the sample code ```$(REPOSITORY_ROOT)/python/shopping/content/product/sample.py``` is retrieved by the registered domain of the account. Owning to the lack of the used domain, for convenience, we modify the code in line 31 as ```python= # website_url = config.get('websiteUrl', 'http://my-book-shop.com') website_url = 'http://my-book-shop.com' ``` But the uploaded products will not pass the audit due to the incorrespondence between the registered domain and website url. ![](https://i.imgur.com/gAzsbHg.png) ### ```products.insert``` The API [products.insert](https://developers.google.com/resources/api-libraries/documentation/content/v2.1/python/latest/content_v2.1.products.html#insert) is used to insert a product. In the sample code ```$(REPOSITORY_ROOT)/python/shopping/content/product/sample.py```, the product is a ```dict``` variable, where the value associated with key ```shipping``` is a list. Please see [the page](https://support.google.com/merchants/answer/7052112?hl=zh-Hant) for details of the specification of uploaded products. Then, go to the directory ```$(REPOSITORY_ROOT)/python``` and run the ```shopping/content/products/insert.py``` file. ```bash= python -m shopping.content.products.insert ``` The retruned message is shown as ![](https://i.imgur.com/hDBhDkJ.png) We can see the item ```book#15705894731``` was created. We can run ```products.list``` to check whether the item has been inserted to the Merchant Center. ![](https://i.imgur.com/STqmcJh.png) The result shows that there is an item in the product list. ### ```products.custombatch``` The API [products.custombatch](https://developers.google.com/resources/api-libraries/documentation/content/v2.1/python/latest/content_v2.1.products.html#custombatch) can retrieve, insert, and delete multiple products in a single request. The action is defined by the key ```method``` of the argument ```body```. #### Run ```products.insert_batch.py``` Similar to what we did in issuing ```products.insert``` earlier, we run ```bash= python -m shopping.content.products.insert_batch ``` The retruned message is shown as ![](https://i.imgur.com/Cyg64Uk.png) We can see that all the products fail to pass the audit: ![](https://i.imgur.com/iX7rtAd.png) #### Run ```products.insert_batch_json.py``` Furthermore, we can insert products from a feed in json format in a single request. Firstly, write a json file in ```$(HOME)/shopping-samples/content/products_list.json```. Modify the feed path variable ```json_path``` in ```$(REPOSITORY_ROOT)/python/shopping/content/products/insert_batch_json.py``` and issue ```bash= python -m shopping.content.products.insert_batch ``` ### ```products.get``` The API [products.get](https://developers.google.com/resources/api-libraries/documentation/content/v2.1/python/latest/content_v2.1.products.html#get) can retrieve a corresponding product given **REST ID** as the query. The **REST ID** for a product is of the form: ```${channel}:${contentLanguage}:${targetCountry}:${offerId}```. For example, we get the item with REST ID ```online:en:US:book#15795934804``` by executing ```python= merchant_id = "142437616" product_id = "online:en:US:book#15795934804" product = service.products().get(merchantId=merchant_id, productId=product_id).execute() ``` #### Run ```products.update.py``` We can update the information of a product by concatenating ```products.update``` and ```products.insert``` sequentially. Check the implementation in ```$(REPOSITORY_ROOT)/python/shopping/content/products/update``` and run the code by issuing ```bash= python -m shopping.content.products.update online:en:US:book#15795934804 ``` ![](https://i.imgur.com/tlWEezw.png) ## API: Account Tax Please see the [page](https://developers.google.com/shopping-content/v2/how-tos/account-level-tax-shipping?hl=zh-CN) for details of settings rules of account tax and shipping setting. ### Evelation of privilege Only the administrator for the account can access the account tax. Since the service account for Cotent API is not an administrator, we should go to the page for account access managment and elevate the privilege. ![](https://i.imgur.com/P0KPkwr.png) Then we can check the privilege of each user. ![](https://i.imgur.com/D7VBVva.png) ### ```accounttax.get``` The API [accounttax.get](https://developers.google.com/resources/api-libraries/documentation/content/v2.1/python/latest/content_v2.1.accounttax.html#get) can retrieve a tax settings of the account. Run the python file by issuing ```bash= python -m shopping.content.accounttax.get ``` Then the result is shown as ![](https://i.imgur.com/CJFfvdS.png) ### ```accounttax.update``` The API [accounttax.update](https://developers.google.com/resources/api-libraries/documentation/content/v2.1/python/latest/content_v2.1.accounttax.html#update) can update the tax settings of the account. Run the python file by issuing ```bash= python -m shopping.content.accounttax.update ``` Then the result is shown as ![](https://i.imgur.com/1Wu3KQI.png) We can create a json file for account tax rules in ```$(HOME)/shopping-samples/content/accounttax_rules.json``` and then run `update_json.py` by issuing ```bash= python -m shopping.content.accounttax.update_json ``` The result is shown as ![](https://i.imgur.com/1qUWBVo.png) In this json file, we create two rules: the first one using global tax table rate in 21167 in US; the rest using 2.15% for 21137 in US. ## API: Shipping Settings Similar to account tax, please see the [page](https://developers.google.com/shopping-content/v2/how-tos/account-level-tax-shipping?hl=zh-CN) for details of settings rules of account tax and shipping setting. Reminder: the privilege should be also elevated. ### ```shippingsettings.get``` The API [shippingsettings.get](https://developers.google.com/resources/api-libraries/documentation/content/v2.1/python/latest/content_v2.1.shippingsettings.html#get) can retrieve a shipping settings of the account. Run the python file by issuing ```bash= python -m shopping.content.shippingsettings.get ``` Then the result is shown as ![](https://i.imgur.com/D8dzryw.png) ### ```shippingsettings.update``` The API [accounttax.update](https://developers.google.com/resources/api-libraries/documentation/content/v2.1/python/latest/content_v2.1.accounttax.html#update) can update the tax settings of the account. We can create a json file for complex shipping settings in ```$(HOME)/shopping-samples/content/shippingsettings_rules.json``` and then run `update_json.py` by issuing ```bash= python -m shopping.content.shippingsettings.update_json ``` The result is shown as ![](https://i.imgur.com/4jMT6GK.png) ## Merchant Items List for GSA testing | Imgs | Description | |------------------------------------|--------------------------------| |![](https://i.imgur.com/CMmLUId.jpg)|Logitech M325 滑鼠 5099206026094 | |![](https://i.imgur.com/r6EJX3E.jpg)|【Vertagear】 SL4000 美國人體工學電競椅 黑藍 0617724127694 | |![](https://i.imgur.com/gZZEvCq.jpg)|飛利浦 BTD7170 DVD 超迷你音響 08712581685157| |![](https://i.imgur.com/oW1K0MO.jpg)|飛利浦 空氣清淨機AC4374/80 08710103699927| |![](https://i.imgur.com/ZNsfexN.jpg)|白蘭氏 傳統雞精70g*12入/盒 4710777112665|