【问题标题】:OAuth2Decorator oauth_aware forces authenticationOAuth2Decorator oauth_aware 强制认证
【发布时间】:2013-01-05 06:41:12
【问题描述】:

我对@9​​87654321@ 和oauth_required 之间区别的理解是aware 不会强制授权,而required 会强制授权,但这不是我在实践中看到的。我有下面两个 webapp RequestHandlers,其中一个get() 方法用decorator.oauth_aware 装饰,另一个用decorator.oauth_required 装饰。但是,当我在本地或 App Engine 上运行时,两者都会立即重定向到登录流程。

目标是SplashHandler 为用户提供一个链接以进行授权,如果他们还没有,则转发到/tasks/

decorator = OAuth2Decorator(
    client_id=settings.CLIENT_ID,
    client_secret=settings.CLIENT_SECRET,
    scope=settings.SCOPE,
    user_agent='mytasks')

class SplashHandler(webapp.RequestHandler):
  @decorator.oauth_aware
  def get(self):
    if not decorator.has_credentials():
      self.response.out.write(template.render('templates/convert.html',
        {'authorize_url': decorator.authorize_url()}))
    else:
      self.redirect('/tasks/')

class TasksHandler(webapp.RequestHandler):
  @decorator.oauth_required
  def get(self):
    tasks = get_tasks()
    tasks.sort(key=lambda x: x['due'])
    self.response.out.write(template.render('templates/index.html',
                                              {'tasks': tasks}))

application = webapp.WSGIApplication(
    [('/', SplashHandler), ('/tasks/', TasksHandler)], debug=True)

【问题讨论】:

    标签: google-app-engine oauth-2.0


    【解决方案1】:

    oauth_aware 方法旨在明确回答“我们是否有当前用户的访问令牌?”这个问题。它可以回答这个问题的唯一方法是知道当前用户是谁,并使用应用程序引擎用户 api 来做到这一点,它本身需要权限提示才能通过您看到的重定向获取您的电子邮件/用户 ID。使用 oauth_required 您实际上会获得 2 个重定向,一个是同一个应用引擎,然后是一个请求 G+ 或 Docs 或其他权限的 oauth。

    我碰巧认为这不是特别有用,我认为您的用例更为常见,但显然库作者不同意。

    也就是说,oauth_aware 函数内部的代码并不是很复杂,您可以基于它制作自己的装饰器,不会进行第一次重定向。不同之处在于,在您的情况下,对同一问题的答案将是“是”或“我不知道”,而不是确定的“否”。

    【讨论】:

    • 谢谢你——这很有意义。我同意我的用例可能更常见,但我有偏见(:我会查看oauth_aware 的源代码并自己编写。再次感谢!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-12-30
    • 2016-01-04
    • 2014-01-09
    • 2013-10-04
    • 1970-01-01
    • 2016-11-09
    • 2016-07-02
    相关资源
    最近更新 更多