简体   繁体   中英

Accessing Google Shopping API via Python

I am quite new to Google APIs and I have been a bit lost :)

First I played around with the Google APIs Explorer and everything was working fine, all my requests.

Then I starting writing a small Python Script to authorise and call the API through a Service Account.

It looks like this:

from apiclient.discovery import build
from oauth2client.service_account import ServiceAccountCredentials

import httplib2
import json
import os


credentials = ServiceAccountCredentials.from_json_keyfile_name(
    filename='/Users/Shopping Dashboard.json',
    scopes='https://www.googleapis.com/auth/content')

http = credentials.authorize(httplib2.Http())

service = build('content', 'v2', http=http)
print service.accounts().authinfo().execute()

request = service.accounts().list(merchantId='xxx', maxResults=50)
result = request.execute()
print result

However although I though it was kind of OK I still get this error

googleapiclient.errors.HttpError: <HttpError 401 when requesting https://www.googleapis.com/content/v2/xxxxx/accounts?alt=json&maxResults=50 returned "User cannot access account xxxxx">

I think what I miss here is kind of creating my token (?) before calling the url? But how is this created? I know there are 1000 examples out there, and I used some scripts that are already in github and they work but they are already too sophisticated and complicated for me to understand. What I miss here is to understand how this process works with some simple example and be able to replicate the logic..

Thanks a lot!

It's probably a token thing. When using this type of APIs, a token is always necessary. but check this to see how it's done using the urllib2.

I had the same problem. Turns out your "Shopping Dashboard.json" file must come from merchant center itself, as opposed to the google developer console.

  • Make sure you have admin access
  • In Merchant Center go to the ... drop down menu > Content API
  • Click the Authentication tab & Generate API key. You'll only be able to see this tab if you have admin access.
  • Save content-api-key.json in place of /Users/Shopping Dashboard.json

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM