authlib icon indicating copy to clipboard operation
authlib copied to clipboard

New token will not be fetched if grant_type='client_credentials' is passed for fetch_token()

Open bryan-prime opened this issue 1 year ago • 0 comments

Describe the bug

If I pass client_credentials as the grant_type it will not automatically fetch the new token.

To Reproduce

My code where I pass the grant_type.

self.oauth2client = OAuth2Session(token_endpoint=f"{base_url}/auth/token")
self.oauth2client.fetch_token(grant_type="client_credentials",)

https://github.com/authlib/authlib/blob/master/authlib/oauth2/client.py#L199-L204

self.metadata is only set if None was passed for grant_type

https://github.com/authlib/authlib/blob/master/authlib/oauth2/client.py#L279-L284

self.metadata['grant_type'] will be None so it will not fetch the new token. self.metadata['grant_type'] needs to be set in order to re-auth without a refresh token.

My workaround was passing nothing for fetch token because it luckily defaults to client_credentials if nothing was passed for the grant_type.

Note: This behavior doesn't come up if there are refresh tokens used for auth because it will just use the refresh token. https://github.com/authlib/authlib/blob/master/authlib/oauth2/client.py#L276

Expected behavior

A clear and concise description of what you expected to happen.

Environment:

  • OS: OSX
  • Python Version: 3.11
  • Authlib Version: 1.3.1

Additional context

Add any other context about the problem here.

bryan-prime avatar Jul 16 '24 02:07 bryan-prime