Search code examples
oauth-2.0access-tokenrefresh-tokenauthlib

Obtaining Refresh Token from lepture/Authlib through Authorization Code


I am trying to develop a simple tool that uses Authlib OAuth2 server to get refresh tokens but example server here does not issue a refresh token. When I print the token I get the following:

{'access_token': '....', 'scope': 'profile', 'token_type': 'Bearer', 'expires_in': 864000, 'expires_at': 1532191887}

The flow is Authorization code as referred here; first I handle the consent part:

client_id = '...'
client_secret = '.....'
scope = '...'
session = OAuth2Session(client_id, client_secret, scope=scope)
authorize_url = '.../oauth/authorize'
uri, state = session.authorization_url(authorize_url)

Then I try to get the token:

urlset = '.../?code=...&state=...'
access_token_url = '.../oauth/token'
token = session.fetch_access_token(access_token_url,authorization_response=urlset)    

Solution

  • Add a config of OAUTH2_REFRESH_TOKEN_GENERATOR=True, added in commit:

    https://github.com/authlib/example-oauth2-server/commit/4f2f48cc3e74b631d9c4c3bdf8558da3de7365a2

    See documentation: https://docs.authlib.org/en/latest/flask/2/