使用Python gdata和oAuth 2对Calendar进行身份验证
我正在将一个
Python应用程序从oAuth 1迁移到oAuth 2,它读取用户的Google
日历Feed. >使用oAuth 1: 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: 这个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 – 假设我可以成功完成上述操作,我可以在DB中保存访问/刷新令牌.使用python gdata lib,我如何使用refresh_token请求另一个access_token(因此每次使用应用程序授权访问时都不必询问用户) 非常感谢! 中号 解决方法
Marchie,
我没有看到您的堆栈跟踪的其余部分,但可以给出三个特定问题,相应的解决方案将解决您的整体问题. 问题I:未在对象上设置值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' 问题II:redirect_uri的默认值不正确(更具体地说,不推荐使用). 由于您在没有参数的情况下调用了generate_authorize_url,因此使用了redirect_uri的默认值,该值当前为oob.作为OAuth 2.0 docs状态,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') 问题III:您使用不正确的值调用get_access_token(也是未在代码段中实例化的值). 您应该使用授权后收到的代码的字符串值或使用“代码”作为键的字典来调用它. 这可以通过以下方式完成: 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的作者也发布了使用该补丁的blog post. (注意,在generate_authorize_url函数中使用关键字redirect_url而不是redirect_uri时,帖子中会出现拼写错误.) (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
- 使用django-allauth注销消息并重定向到主页
- python通过pil将图片转换成黑白效果
- numpy 进行数组拼接,分别在行和列上合并的实例
- 在OS X 10.7上,NumPy不会从1.5.1升级到1.6.2
- 剖析Django中模版标签的解析与参数传递
- django – 使用update_index –remove从Haystack / Xapian索
- 五 .Django 静态文件 static
- 什么是python相当于useAutomationExtension for selenium?
- Python使用matplotlib的pie函数绘制饼状图功能示例
- python3 利用栈 将十进制转换为二进制,及十六以下任意进制