使用 Python gdata 和 oAuth 2 对日历进行身份验证

2024-05-08

我正在将一个 Python 应用程序从 oAuth 1 迁移到 oAuth 2,该应用程序读取用户的 Google 日历提要。

  • 使用 oAuth 1: 如果用户可以使用他的 GMail 进行身份验证,我的应用程序将打开浏览器 帐户并授权访问,我的应用程序将获得 user_token, 该用户的 user_secret,然后对日历源进行身份验证:

    client = gdata.calendar.client.CalendarClient(source='test')
    client.auth_token = gdata.gauth.OAuthHmacToken(app_key,
             app_secret,user_token,user_secret,gdata.gauth.ACCESS_TOKEN)
    

这对象征性的秘密配对将长期存在。

  • 使用 oAuth 2: 我在 Google API 控制台中注册了我的应用程序并获得了 oAuth 2 client_id 和 client_secret,并修改应用程序以请求 用户的access_token、refresh_token来自https://accounts.google.com/o/oauth2/token https://accounts.google.com/o/oauth2/token对于 GData 库,我应用了此处指定的 gauth.py 补丁:http://codereview.appspot.com/4440067/ http://codereview.appspot.com/4440067/

此 access_token 的寿命很短。

我玩了一下这里发布的代码http://codereview.appspot.com/4440067/ http://codereview.appspot.com/4440067/并且工作正常。

我的问题:

-我通过我的curl调用获取access_token、refresh_token 应用程序,我可以成功检索两者。但是,当我将其应用到 这段代码:

    token =
    gdata.gauth.OAuth2Token(client_id=client_id,client_secret=client_secret',
                           scope='https://www.google.com/calendar/
    feeds',user_agent='calendar-cmdline-sample/1.0')
    uri = token.generate_authorize_url()
    token.get_access_token(access_token)

它给了我:

Traceback (most recent call last):
 File "<stdin>", line 1, in <module>
 File "/Library/Python/2.6/site-packages/gdata/gauth.py", line 1267,
in get_access_token
   raise OAuth2AccessTokenError(error_msg)
gdata.gauth.OAuth2AccessTokenError

-假设我可以成功完成上述操作,我可以将访问/刷新令牌保存在数据库中。使用python gdata lib,如何使用refresh_token请求另一个access_token(因此不必每次使用应用程序时都询问用户授权访问它)

提前非常感谢!

M


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功能。)

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

使用 Python gdata 和 oAuth 2 对日历进行身份验证 的相关文章

随机推荐