14

我正在尝试在服务器到服务器 JSON API 场景中创建一个 oauth2 access_token。但它因 invalid_grant 错误而失败,请帮忙。

from oauth2client.client import SignedJwtAssertionCredentials

KEY_FILE = 'xxxxxxxxxxxx-privatekey.p12'

with open(KEY_FILE, 'r') as fd:
    key = fd.read()

SERVICE_ACCOUNT_EMAIL = 'xxxxxx.apps.googleusercontent.com'

credentials = SignedJwtAssertionCredentials(SERVICE_ACCOUNT_EMAIL, key,
      scope="https://www.googleapis.com/auth/datastore https://www.googleapis.com/auth/userinfo.email",
      token_uri='https://accounts.google.com/o/oauth2/token')


assertion = credentials._generate_assertion()

h = httplib2.Http()
credentials._do_refresh_request(h.request)

我得到了

Traceback (most recent call last):
  File "/Users/pahud/Projects/oauth2client/x.py", line 24, in <module>
    credentials._do_refresh_request(h.request)
  File "/Users/pahud/Projects/oauth2client/oauth2client/client.py", line 710, in _do_refresh_request
    raise AccessTokenRefreshError(error_msg)
oauth2client.client.AccessTokenRefreshError: invalid_grant
[Finished in 0.7s with exit code 1]

http://i.stack.imgur.com/iGGYx.png

4

3 回答 3

12

我修好了它。

SERVICE_ACCOUNT_EMAIL = 'xxxxxx.apps.googleusercontent.com'

以上是客户 ID 而不是电子邮件,我已修复此问题,现在可以使用。

于 2014-05-29T10:42:30.427 回答
2

我也有同样的问题。

要解决这个问题,您需要注意以下要素:

  1. 你在你的程序中使用了 client_secrets.json 吗?如果是,请检查名称是否与您当前目录中的名称相同。

  2. " client_email" 或 " SERVICE_ACCOUNT_EMAIL" 不是您的个人电子邮件或客户 ID。它是“客户 ID 的电子邮件”。您可以在https://console.developers.google.com/project/ 中查看该电子邮件==>credentials==>Service account==>email address

    基本上,如果您的客户 ID 是:<clientid>.apps.googleusercontent.com

    您的客户电子邮件将是:<clientid>@developer.gserviceaccount.com

于 2015-07-28T17:04:46.253 回答
0

就我而言,问题出在 .boto 文件上。尝试使用服务帐户中的凭据再次对其进行配置。

对于使用后备的人:gcs_oauth2_boto_plugin.SetFallbackClientIdAndSecret(CLIENT_ID, CLIENT_SECRET)

使用任何“本机应用程序的客户端 ID”作为后备。这不是必要的,因为它在:https ://cloud.google.com/storage/docs/gspythonlibrary

但我找不到其他方法,没有它就会抛出错误。

于 2015-06-29T00:12:53.713 回答