我对 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 之前创建我的 token (?)?但这是如何创建的呢?我知道那里有 1000 个示例,我使用了一些已经在 github 中的脚本,它们可以工作,但它们已经太复杂了,我无法理解。我在这里想念的是通过一些简单的例子来理解这个过程是如何工作的,并能够复制逻辑。
非常感谢!
请您参考如下方法:
我遇到了同样的问题。事实证明,您的“Shopping Dashboard.json”文件必须来自 Merchant Center 本身,而不是来自 Google 开发者控制台。
- 确保您拥有管理员权限
- 在 Merchant Center 中转到 ... 下拉菜单 > Content API
- 点击身份验证选项卡并生成 API key 。只有拥有管理员权限才能看到此标签。
- 保存 content-api-key.json 代替/Users/Shopping Dashboard.json
