【问题标题】:Authenticating to Calendar with Python gdata and oAuth 2使用 Python gdata 和 oAuth 2 对日历进行身份验证
【发布时间】: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)
    

这个令牌,秘密对将长期存在。

这个 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

【问题讨论】:

    标签: python gdata oauth-2.0


    【解决方案1】:

    马奇,

    我没有看到您的堆栈跟踪的其余部分,但可以给出三个特定问题以及相应的解决方案来解决您的整体问题。

    问题一:对象上没有设置值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 docs 状态,oob 不在受支持的值之列(已弃用)。

    如果您确实在使用已安装的应用程序,则需要将其设置为

    token.redirect_uri = 'urn:ietf:wg:oauth:2.0:oob'
    

    另外,当你调用generate_authorize_url获取初始token时,你需要使用这个作为关键字参数

    url = token.generate_authorize_url(redirect_uri='urn:ietf:wg:oauth:2.0:oob')
    

    问题 III:您调用 get_access_token 时使用了不正确的值(也是未在您的代码 sn-p 中实例化的值)。

    您应该使用授权后收到的代码的字符串值或使用以'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 的作者还发布了blog post 使用补丁。 (请注意,在 generate_authorize_url 函数中使用关键字 redirect_url 而不是 redirect_uri 时,帖子中有错字。)

    【讨论】:

      猜你喜欢
      • 2018-06-07
      • 1970-01-01
      • 1970-01-01
      • 2019-09-03
      • 2012-05-29
      • 1970-01-01
      • 2017-10-31
      • 2023-03-12
      • 1970-01-01
      相关资源
      最近更新 更多