【问题标题】:Does authlib support refresh my access token when using client credentials?使用客户端凭据时,authlib 是否支持刷新我的访问令牌?
【发布时间】:2020-05-31 04:53:29
【问题描述】:

我是 authlib 的新手,在查看文档时提到它支持 passwordgrant_type 的“自动刷新”。

我想知道库是否能够在使用client_credentials 时检测到当前关联的令牌何时过期,然后为我请求一个新的访问令牌。 (为我完成整个 OAuth 舞蹈)

检查我需要做的每个调用,我是否收到 401,然后触发获取新令牌的操作,这似乎很难看。

这里有最佳实践吗?可以以某种方式将其添加为装饰器吗?

【问题讨论】:

    标签: python-3.x oauth-2.0 authlib


    【解决方案1】:

    为了给前面的答案增添一些风味,我能够让 refresh_token 函数为最近的 API 集成工作。

    这有一些特殊性,因为我必须在 refresh_token 调用中传递 client_id 和 client_secret。我还必须设置grant_type。这些可能不适用于您尝试使用的 API。

    from authlib.integrations.requests_client import OAuth2Session
    
    my_token = {'refresh_token':current_refresh_token,
                'access_token': current_access_token,
                'expires_at': current_access_token_expires_at,
                'expires_in': current_access_token_expires_in}
    
    oauth_session = OAuth2Session(CLIENT_ID,CLIENT_SECRET,
                                  authorization_endpoint=AUTH_ENDPOINT,
                                  token_endpoint=TOKEN_ENDPOINT,
                                  token=my_token,
                                  grant_type='refresh_token')
    
        new_token = oauth_session.refresh_token(
                        url=REFRESH_TOKEN_URL,
                        client_id=CLIENT_ID,
                        client_secret=CLIENT_SECRET)
    

    【讨论】:

      【解决方案2】:
      猜你喜欢
      • 2019-01-08
      • 2019-05-24
      • 2022-10-08
      • 2018-12-14
      • 2017-09-06
      • 2021-09-01
      • 2011-04-20
      • 2017-05-01
      • 2014-03-19
      相关资源
      最近更新 更多