我是Google API的新手,我有点迷茫了:)
首先,我试用了一下Google APIs Explorer,所有的请求都运行得很好。
然后,我开始编写一个小的Python脚本,以便通过服务帐户授权和调用API。
它看起来是这样的:
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然而,尽管我认为这是一种OK,但我仍然得到这个错误
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">我想我在这里错过的是创建我的令牌(?)在调用url之前?但是这是如何产生的呢?我知道有1000个例子,我使用了github中已经存在的一些脚本,它们可以工作,但它们已经太复杂了,我无法理解。我在这里缺少的是通过一些简单的例子来理解这个过程是如何工作的,并且能够复制逻辑。
非常感谢!
发布于 2018-03-27 17:44:18
我也有同样的问题。原来你的“购物Dashboard.json”文件必须来自商业中心本身,而不是谷歌开发人员控制台。
中保存content-api-key.json
发布于 2017-03-08 23:10:20
可能是象征性的东西。在使用此类API时,token始终是必需的。但是可以查看一下this,看看它是如何使用urllib2完成的。
https://stackoverflow.com/questions/42674796
复制相似问题