python - 带有 OAuth 和 Python 的 Gmail API - AccessTokenRefreshError : invalid_grant

标签 python oauth google-api gmail-api

我正在尝试将 Gmail API 与 Python 客户端库一起使用。但是,我遇到了有关 AccessTokenRefreshError: invalid_grant 的错误消息。

我已经这样安装了 Google 客户端库:

pip install google-api-python-client

我去了Google Developer's Console ,创建了一个新项目,然后转到 APIs & auth,然后是 Credentials。然后,我点击了创建新的客户端 ID,并选择了服务帐户

起初,当我运行以下命令时,它提示没有加密库,所以我 pip 安装了 pycrypto。然后,它提示 key 文件格式错误,所以我 pip 安装了 pyopenssl

然后,我在 Python shell 中运行:

from oauth2client.client import SignedJwtAssertionCredentials
client_email = '<SANITISED>.apps.googleusercontent.com'
with open("foobar-d31647e3d00a.p12") as f:
    private_key = f.read()
    credentials = SignedJwtAssertionCredentials(client_email, private_key, 'https://www.googleapis.com/auth/gmail.readonly')

from httplib2 import Http
http_auth = credentials.authorize(Http())
from apiclient.discovery import build
gmail_server = build('gmail', 'v1', http=http_auth)

当我运行最后一条命令时,我得到了以下堆栈跟踪:

AccessTokenRefreshError                   Traceback (most recent call last)
<ipython-input-8-7fd72f40edd2> in <module>()
----> 1 gmail_server = build('gmail', 'v1', http=http_auth)

/Users/victorhooi/.virtualenvs/kenny/lib/python2.7/site-packages/oauth2client/util.pyc in positional_wrapper(*args, **kwargs)
    133         else: # IGNORE
    134           pass
--> 135       return wrapped(*args, **kwargs)
    136     return positional_wrapper
    137

/Users/victorhooi/.virtualenvs/kenny/lib/python2.7/site-packages/googleapiclient/discovery.pyc in build(serviceName, version, http, discoveryServiceUrl, developerKey, model, requestBuilder, credentials)
    196   logger.info('URL being requested: GET %s' % requested_url)
    197
--> 198   resp, content = http.request(requested_url)
    199
    200   if resp.status == 404:

/Users/victorhooi/.virtualenvs/kenny/lib/python2.7/site-packages/oauth2client/util.pyc in positional_wrapper(*args, **kwargs)
    133         else: # IGNORE
    134           pass
--> 135       return wrapped(*args, **kwargs)
    136     return positional_wrapper
    137

/Users/victorhooi/.virtualenvs/kenny/lib/python2.7/site-packages/oauth2client/client.pyc in new_request(uri, method, body, headers, redirections, connection_type)
    528       if not self.access_token:
    529         logger.info('Attempting refresh to obtain initial access_token')
--> 530         self._refresh(request_orig)
    531
    532       # Clone and modify the request headers to add the appropriate

/Users/victorhooi/.virtualenvs/kenny/lib/python2.7/site-packages/oauth2client/client.pyc in _refresh(self, http_request)
    742     """
    743     if not self.store:
--> 744       self._do_refresh_request(http_request)
    745     else:
    746       self.store.acquire_lock()

/Users/victorhooi/.virtualenvs/kenny/lib/python2.7/site-packages/oauth2client/client.pyc in _do_refresh_request(self, http_request)
    805       except (TypeError, ValueError):
    806         pass
--> 807       raise AccessTokenRefreshError(error_msg)
    808
    809   def _revoke(self, http_request):

AccessTokenRefreshError: invalid_grant

对我做错了什么有什么想法吗?

最佳答案

啊哈,我发现了问题 - 我的愚蠢。

我使用的是“客户端 ID”,并将其传递到 SignedJwtAssertionCredentials

我应该一直使用“电子邮件地址”,而不是传递它。

您可能认为变量名为 client_email 的事实会是一个线索……哈哈。

作为引用,相关的 API 文档页面:

http://google-api-python-client.googlecode.com/hg/docs/epy/oauth2client.client.SignedJwtAssertionCredentials-class.html

关于python - 带有 OAuth 和 Python 的 Gmail API - AccessTokenRefreshError : invalid_grant,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27810053/

相关文章:

python - Python 中的简单合并排序错误

python - key 错误 : 'darkblue' when using trepan3k

android - LinkedIn 抄写员 OAuth 库无法允许访问 LinkedIn 帐户

ios - 获取 google people profile API 不返回生日和性别键值

python - 不确定如何计算要显示的特定值(Python/Django)

python - 无法执行基于 Python 的 Hadoop Streaming 作业

c# - OAuth 不记名 token 身份验证未通过签名验证

java - 在 java 中获取谷歌的电子邮件/用户名?

google-api - 如何在我的网站上使用 Google 文档编辑服务?

c# - 如何通过 Google Drive .NET API v3 使用服务帐户访问 Team Drive