OAuth2Decorator oauth_aware强制进行身份验证

Hal*_*own 5 google-app-engine oauth-2.0

我的区别的理解之间oauth_aware,并oauth_requiredaware不强制授权,同时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)
Run Code Online (Sandbox Code Playgroud)

Gre*_*reg 7

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

我忽然想到,这是不是特别有用,我觉得你的用例是更常见的,但显然该库的作者不同意.

说,oauth_aware函数中的代码不是很复杂,你可以根据它做不自己的第一次重定向装饰器.不同之处在于,在您的情况下,对同一问题的回答将是"是"或"我不知道",而不是确定的"否".