0

我想删除登录用户的令牌和会话,这样当用户再次尝试登录时,它会再次显示他的谷歌登录页面。我跟着烧瓶舞文件。但是当我尝试注销时,它显示 None 类型不可下标,并且它也没有删除会话。您可以在下面找到我的代码片段。

google_blueprint = make_google_blueprint(client_id='',
                                         client_secret='', offline=True,
                                         scope=['profile', 'email'])
app.register_blueprint(google_blueprint, url_prefix='/login')

@app.route('/login')
def google_login():

    if not google.authorized:
        return redirect(url_for('google.login'))

@oauth_authorized.connect_via(google_blueprint)
def logged_in(blueprint, token):
    if not token:
        flash("Failed to log in with GitHub.", category="error")
        return False
    account_info = blueprint.session.get('/oauth2/v1/userinfo')
    print(token)
    if account_info:
        account_info_json = account_info.json()
        Email = account_info_json['email']
        user = User.query.filter_by(email=Email).first()

        if user:
            try:
                login_user(user)
                return redirect(url_for('welcome'))
            except BaseException():
                return redirect(url_for('google.login'))

        else:
            return redirect(url_for('logout'))
    return render_template('home.html')

@app.route("/logout")
def logout():

    token = google_blueprint.token["access_token"] #showing error in this line
    resp = google.post(
        "https://accounts.google.com/o/oauth2/revoke",
        params={"token": token},
        headers={"Content-Type": "application/x-www-form-urlencoded"}
    )
    assert resp.ok, resp.text
    logout_user()        # Delete Flask-Login's session cookie
    del google_blueprint.token  # Delete OAuth token from storage
    return redirect('/')

我在注销路线的第一行收到错误。如何解决这个问题?

4

1 回答 1

0

如果blueprint.token为 None,则表示令牌存储系统中没有 OAuth 令牌。通常,这表示没有保存的令牌可撤销。如果您遇到此问题,则可能表明您一开始就没有登录用户——如果您没有登录,那么注销是没有意义的!

另请参阅OAuth2ConsumerBlueprint.token.

于 2020-09-24T14:15:52.807 回答