【发布时间】:2017-07-29 05:24:27
【问题描述】:
我对 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
不过,虽然我觉得还不错,但还是会出现这个错误
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 中的脚本,它们可以工作,但是它们已经太复杂了,我无法理解。我在这里想念的是通过一些简单的例子来理解这个过程是如何工作的,并且能够复制逻辑..
非常感谢!
【问题讨论】: