将Google电子邮件ID与数据库中的电子邮件ID进行比较,然后返回用户详细信息

时间:2018-12-24 06:58:36

标签: mongodb flask google-oauth2

我有一个数据库,其中包含一些用户详细信息,例如姓名电子邮件密码等 当我使用邮件帐户登录时,我需要检查登录的gmail ID是否在数据库中,以及是否已获取该特定用户的详细信息并将用户重定向到用户个人资料

我已经获得了Gmail帐户使用范围https://www.googleapis.com/oauth2/v1/userinfo

所提供的电子邮件ID和其他用户详细信息
@app.route('/')
def index():
    access_token = session.get('access_token')
    if access_token is None:
        return redirect(url_for('login'))

    access_token = access_token[0]
    from urllib.request import Request, urlopen, URLError

    headers = {'Authorization': 'OAuth '+access_token}
    req = Request('https://www.googleapis.com/oauth2/v1/userinfo',
    None, headers)  
    try:
        res = urlopen(req)
    except URLError as e:
        if e.code == 401:
            # Unauthorized - bad token
            session.pop('access_token', None)
            return redirect(url_for('login'))
        return res.read()
    return res.read()

@app.route('/login')
def login():
    callback=url_for('authorized', _external=True)
    return google.authorize(callback=callback)

@app.route(REDIRECT_URI)
@google.authorized_handler
def authorized(resp):
    access_token = resp['access_token']
    session['access_token'] = access_token, ''
    return redirect(url_for('index'))

@google.tokengetter
def get_access_token():
    return session.get('access_token')


I need to compare or check if the logged in email id user exists in the database and get that particular user's details and display it to profile page

0 个答案:

没有答案