【问题标题】:got 'invalid_grant' in oauth2 SignedJwtAssertionCredentials在 oauth2 SignedJwtAssertionCredentials 中得到“invalid_grant”
【发布时间】:2023-03-10 18:35:01
【问题描述】:

我正在尝试在服务器到服务器 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

【问题讨论】:

  • 电子邮件范围需要以“https”(而不是“http”)开头。这能解决问题吗?
  • @EdDavisson 不,仍然得到 invalid_grant :(
  • 也不同步。机器上的时间可能会产生类似的错误。

标签: google-cloud-datastore oauth2client


【解决方案1】:

我修好了。

SERVICE_ACCOUNT_EMAIL = 'xxxxxx.apps.googleusercontent.com'

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

【讨论】:

  • 在我的例子中,我使用的是我的帐户登录电子邮件,而不是您从 Google Developers Console 的凭据部分创建新客户 ID 时获得的服务帐户电子邮件地址
【解决方案2】:

我也有同样的问题。

要解决这个问题,你需要注意以下几点:

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

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

    基本上,如果您的客户 ID 是:&lt;clientid&gt;.apps.googleusercontent.com

    您的客户电子邮件是:&lt;clientid&gt;@developer.gserviceaccount.com

【讨论】:

    【解决方案3】:

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

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

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

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-01-16
      • 1970-01-01
      • 2016-09-23
      • 2014-09-22
      • 2016-03-05
      • 2015-09-17
      • 2021-06-15
      • 2014-03-24
      相关资源
      最近更新 更多