【问题标题】:Accessing Google Shopping API via Python通过 Python 访问 Google Shopping API
【发布时间】: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 中的脚本,它们可以工作,但是它们已经太复杂了,我无法理解。我在这里想念的是通过一些简单的例子来理解这个过程是如何工作的,并且能够复制逻辑..

非常感谢!

【问题讨论】:

    标签: python google-content-api


    【解决方案1】:

    我遇到了同样的问题。结果你的“Shopping Dashboard.json”文件必须来自商家中心本身,而不是谷歌开发者控制台。

    • 确保您拥有管理员权限
    • 在 Merchant Center 中,转到 ... 下拉菜单 > Content API
    • 单击身份验证选项卡并生成 API 密钥。只有拥有管理员权限才能看到此标签。
    • 保存 content-api-key.json 代替 /Users/Shopping Dashboard.json

    【讨论】:

      【解决方案2】:

      这可能是一个象征性的东西。使用此类 API 时,始终需要令牌。但请检查this 以了解它是如何使用 urllib2 完成的。

      【讨论】:

      • 我还是不明白,我不确定在服务帐户中是否真的是这种情况。例如在这段代码中没有令牌:github.com/google/google-api-python-client/blob/master/samples/…
      • 我相信来自 github 的代码示例非常清晰,并且 cmets 信息量很大。但是你有没有试过在不给list方法加参数的情况下运行代码?
      猜你喜欢
      • 2012-04-22
      • 1970-01-01
      • 2014-08-26
      • 1970-01-01
      • 2021-04-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-11-04
      相关资源
      最近更新 更多