【问题标题】:GitHub-Flask Authorize Scope IssueGitHub-Flask 授权范围问题
【发布时间】:2018-05-26 16:11:08
【问题描述】:

我正在使用Github-Flask 对我的应用上的用户进行身份验证。我使用github.authorize(scope='user:email')。如何获取登录用户的邮箱?

github = GitHub(app)
user = None

@app.route('/login')
def login():
   if user.username: 
      return redirect(url_for('index'))

   return github.authorize(scope='user:email')

@github.access_token_getter
def token_getter():
   if user is not None:
      return user.github_access_token

@app.route('/github-callback')
@github.authorized_handler
def authorized(oauth_token):
    if oauth_token is None:
       flask.flash("Authorization failed.")
       return redirect(url_for('index'))

   global user
   user.username = oauth_token
   return redirect(url_for('index'))

【问题讨论】:

    标签: python github flask github-api


    【解决方案1】:

    login 路由只是重定向到 GitHub 的授权页面,它还没有办法访问用户数据,因为用户没有登录。一旦你到达 authorized 回调,你可以进行 API 调用到 GitHub。

    authorized 路由中,使用github.get 调用user API 端点。

    data = github.get('user')
    email = data['email']
    

    另外,不要使用global user 来存储登录用户。参见Are global variables thread safe in flask? 而是将用户id 存储在session 中,并将用户加载到g 中,如GitHub-Flask's full example 所示。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-05-14
      • 2020-09-20
      • 2014-06-30
      • 1970-01-01
      • 1970-01-01
      • 2013-07-19
      相关资源
      最近更新 更多