【问题标题】:flask-dance logout showing none-type is not subscriptable显示无类型的烧瓶舞注销不可下标
【发布时间】:2020-06-01 06:37:31
【问题描述】:

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

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('/')

我在注销路径的第一行收到错误。如何解决?

【问题讨论】:

  • logout 方法中,你能打印出google_blueprint 的值吗?我想会是None
  • 显示错误。'OAuth2ConsumerBlueprint' 对象不可调用
  • 发生这种情况是因为google_blueprint.token 返回None
  • 如何解决这个问题?如何获取token并删除它?flask-dance文档中只提到过。

标签: python flask google-oauth flask-login flask-dance


【解决方案1】:

如果blueprint.token 为None,则表示令牌存储系统中没有OAuth 令牌。通常,这表明没有保存的令牌可撤销。如果您遇到此问题,则可能首先表明您没有登录用户 - 如果您未登录,则注销没有意义!

另见the docs for OAuth2ConsumerBlueprint.token

【讨论】:

    猜你喜欢
    • 2021-08-11
    • 2017-11-09
    • 2018-10-18
    • 1970-01-01
    • 2017-04-15
    • 2019-01-04
    • 1970-01-01
    • 1970-01-01
    • 2021-07-30
    相关资源
    最近更新 更多