【发布时间】:2011-11-04 04:36:13
【问题描述】:
我正在将 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 对于 GData 库,我应用了此处指定的 gauth.py 补丁: http://codereview.appspot.com/4440067/
这个 access_token 是短暂的。
我玩了一下这里发布的代码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
【问题讨论】: