带有OAuth和Python的Gmail API - AccessTokenRefreshError: invalid_



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

我已经安装了谷歌客户端库,如下所示:

pip install google-api-python-client

我去了Google开发人员控制台,创建了一个新项目,然后转到API和身份验证,然后转到凭据。然后,我单击了"创建新的客户端 ID",并选择了"服务帐户"。

起初,当我运行以下内容时,它抱怨没有加密库,所以我 pip 安装了 pycrypto。然后,它抱怨密钥文件的格式错误,所以我 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

相关内容

  • 没有找到相关文章

最新更新