Python GitHub授权范围问题

Python GitHub授权范围问题,python,github,flask,github-api,Python,Github,Flask,Github Api,我正在使用对我的应用程序上的用户进行身份验证。我使用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:ema

我正在使用对我的应用程序上的用户进行身份验证。我使用
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'))

登录
路由只是重定向到GitHub的授权页面,它还无法访问用户数据,因为用户尚未登录。一旦进入
authorized
回调,就可以对GitHub进行API调用

authorized
路由中,使用调用API端点

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

另外,不要使用
全局用户
存储登录的用户。请参见,将用户id存储在
会话中
,并将用户加载到
g
,如中所示