Marchie,
我看不到您的堆栈跟踪的其余部分,但可以给出三个特定问题以及相应的解决方案,以解决您的整体问题。
问题一: 价值redirect_uri
未在对象上设置。
请注意请求正文是如何指定的get_access_token
:
body = urllib.urlencode({
'grant_type': 'authorization_code',
'client_id': self.client_id,
'client_secret': self.client_secret,
'code': code,
'redirect_uri': self.redirect_uri,
'scope': self.scope
})
这取决于redirect_uri
对象上的属性设置为最初设置的值generate_authorize_url
。因此,在通过调用重建令牌之后
token = gdata.gauth.OAuth2Token(...)
您只需要设置重定向 URI:
token.redirect_uri = 'http://path/that/you/set'
问题二:默认值redirect_uri
是不正确的(更具体地说,已弃用)。
自从你打电话以来generate_authorize_url
不带参数的情况下,默认值redirect_uri
已使用,目前是oob
。作为OAuth 2.0 文档 https://developers.google.com/accounts/docs/OAuth2InstalledApp#choosingredirecturi州,oob
不属于受支持的值(已被弃用)。
如果您确实使用已安装的应用程序,则需要将其设置为
token.redirect_uri = 'urn:ietf:wg:oauth:2.0:oob'
此外,当您致电generate_authorize_url
要获取初始令牌,您需要将其用作关键字参数
url = token.generate_authorize_url(redirect_uri='urn:ietf:wg:oauth:2.0:oob')
问题三: 你打电话来get_access_token
值不正确(也是尚未在代码片段中实例化的值)。
您应该使用授权后收到的代码的字符串值或包含以下内容的字典来调用此函数:'code'
作为钥匙。
这可以通过以下方式完成:
import atom.http_core
# Page the user is redirected to after authorizing
redirected_page = 'http://path/that/you/set?code=RANDOM-CODE'
uri = atom.http_core.ParseUri(redirected_page)
# uri.query is a dictionary with the query string as key, value pairs
token.get_access_token(uri.query)
后脚本: 该书的作者patch http://codereview.appspot.com/4440067/还发表了博客文章 http://googleappsdeveloper.blogspot.com/2011/09/python-oauth-20-google-data-apis.html关于使用补丁。 (请注意,当关键字出现时,帖子中存在拼写错误redirect_url
被用来代替redirect_uri
in the generate_authorize_url
功能。)