diff --git a/requests_oauthlib/oauth2_session.py b/requests_oauthlib/oauth2_session.py index bfec8e8d..3d690ea3 100644 --- a/requests_oauthlib/oauth2_session.py +++ b/requests_oauthlib/oauth2_session.py @@ -41,6 +41,7 @@ def __init__( client=None, auto_refresh_url=None, auto_refresh_kwargs=None, + auto_refresh_type="refresh_token", scope=None, redirect_uri=None, token=None, @@ -67,6 +68,8 @@ def __init__( your access tokens. :auto_refresh_kwargs: Extra arguments to pass to the refresh token endpoint. + :auto_refresh_type: Type of auto refresh method to use. Must be either + "refresh_token" (default) or "access_token". :token_updater: Method with one argument, token, to be used to update your token database on automatic token refresh. If not set a TokenUpdated warning will be raised when a token @@ -83,6 +86,7 @@ def __init__( self._state = state self.auto_refresh_url = auto_refresh_url self.auto_refresh_kwargs = auto_refresh_kwargs or {} + self.auto_refresh_type = auto_refresh_type self.token_updater = token_updater # Ensure that requests doesn't do any automatic auth. See #278. @@ -499,9 +503,18 @@ def request( client_id, ) auth = requests.auth.HTTPBasicAuth(client_id, client_secret) - token = self.refresh_token( - self.auto_refresh_url, auth=auth, **kwargs - ) + if self.auto_refresh_type == "refresh_token": + token = self.refresh_token( + self.auto_refresh_url, auth=auth, **kwargs + ) + elif self.auto_refresh_type == "access_token": + token = self.fetch_token( + self.auto_refresh_url, + auth=auth, + **dict(kwargs, **self.auto_refresh_kwargs), + ) + else: + raise RuntimeError("Unknown auto_refresh_type: %s" % self.auto_refresh_type) if self.token_updater: log.debug( "Updating token to %s using %s.", token, self.token_updater