【问题标题】:Flask + SQLAlchemy + Google Ads API : How to properly update refresh token?Flask + SQLAlchemy + Google Ads API:如何正确更新刷新令牌?
【发布时间】:2021-08-26 08:53:04
【问题描述】:

# create/login local user on successful OAuth login
@oauth_authorized.connect_via(google)
def google_logged_in(blueprint, token):
    #
    # Token is accessible in this scope with token=xyz
    #
    # One Time Commit to unlock the database
    # db.session.commit()
    ##########################################

    if not token:
        flash("Failed to log in.", category="error")
        return False

    resp = blueprint.session.get("/oauth2/v1/userinfo")
    if not resp.ok:
        msg = "Failed to fetch user info."
        flash(msg, category="error")
        return False

    info = resp.json()
    user_id = str(info["id"])
    
    


    # Find this OAuth token in the database, or create it
    query = OAuth.query.filter_by(provider=blueprint.name, provider_user_id=user_id)
    try:
        oauth = query.one()
        db.session.commit()
    except NoResultFound:
        oauth = OAuth(provider=blueprint.name, provider_user_id=user_id, token=token)
    if oauth.user:
        # If this OAuth token already has an associated local account,
        # log in that local user account.
        # Note that if we just created this OAuth token, then it can't
        # have an associated local account yet.
        #

        try:
            update=OAuth.query.filter_by(provider=blueprint.name, provider_user_id=user_id)
            update.token=token
            db.session.commit()
        except:
            print('cannot update login token')
        
        login_user(oauth.user)
        flash("Successfully signed in.")

    else:
        # Create a new local user account for this user
        user = User(email=info["email"],image_url=info["picture"])
        # Associate the new local user account with the OAuth token
        oauth.user = user
        # Save and commit our database models
        db.session.add_all([user, oauth])
        db.session.commit()
        # Log in the new local user account
        login_user(user)
        flash("Successfully signed in.")

    # Disable Flask-Dance's default behavior for saving the OAuth token
    return False

我想要做的是:

Flowchart of my Process

我是数据库菜鸟,所以我不确定我是否在做我想做的事。

问题是我的令牌过期后我的数据库锁定,所以这部分无法正常工作:

if oauth.user:
        # If this OAuth token already has an associated local account,
        # log in that local user account.
        # Note that if we just created this OAuth token, then it can't
        # have an associated local account yet.
        #

        try:
            update=OAuth.query.filter_by(provider=blueprint.name, provider_user_id=user_id)
            update.token=token
            db.session.commit()
        except:
            print('cannot update login token')

        login_user(oauth.user)
        flash("Successfully signed in.")

但我找不到问题出在哪里。 文档没有帮助,关注 SO 帖子也没有帮助:

How do I unlock a SQLite database?

SQLAlchemy and SQLite: database is locked

https://www.reddit.com/r/flask/comments/36g2g7/af_sqlite_database_locking_problem/

谁能让我摆脱生存危机和 SQL 的愚蠢?

编辑: 令牌过期后,我现在只收到消息: oauthlib.oauth2.rfc6749.errors.InvalidGrantError: (invalid_grant) Token has been expired or revoked.

我更换的时候修了一点

query = OAuth.query.filter_by(provider=blueprint.name, provider_user_id=user_id)

query = db.session.query(OAuth).filter_by(provider=blueprint.name, provider_user_id=user_id) 但我似乎没有将新令牌写入数据库,因为我仍然得到: oauthlib.oauth2.rfc6749.errors.InvalidGrantError: (invalid_grant) Token has been expired or revoked. 所以现在我的问题是如何正确更新刷新令牌?

【问题讨论】:

    标签: python sqlite flask sqlalchemy flask-sqlalchemy


    【解决方案1】:

    我没有发现以下错误:

    update=db.session.query(OAuth).filter_by(provider=blueprint.name, provider_user_id=user_id) update.token=token

    因为 try 语句(我知道它很菜)。我比这样更新了我的代码:

    if oauth.user:
            # If this OAuth token already has an associated local account,
            # log in that local user account.
            # Note that if we just created this OAuth token, then it can't
            # have an associated local account yet.
            #
    
            #try:
            print('Trying to update the token')
            update=db.session.query(OAuth).filter_by(provider=blueprint.name, provider_user_id=user_id)
            update.token=token
            db.session.commit()
            print('User has been updated:', update.token==token)
            #except:
            #    print('cannot update login token')
    
            login_user(oauth.user)
            flash("Successfully signed in.")
    

    发现 AttributeError: 'BaseQuery' object has no attribute 'token'

    快速搜索了一下,发现我需要像这样使用 first():

    update=db.session.query(OAuth).filter_by(provider=blueprint.name, provider_user_id=user_id).first()

    感谢我自己这么菜鸟,仍然得到我的问题的答案。

    【讨论】:

      猜你喜欢
      • 2016-01-26
      • 2016-08-03
      • 2020-02-06
      • 2021-07-11
      • 2021-05-15
      • 2012-02-15
      • 2014-11-25
      • 2016-12-13
      • 2016-08-21
      相关资源
      最近更新 更多