Skip to content

Commit 1224b58

Browse files
committed
Add support for auto-refreshing token without refresh token
1 parent 29ba9af commit 1224b58

File tree

1 file changed

+16
-3
lines changed

1 file changed

+16
-3
lines changed

requests_oauthlib/oauth2_session.py

+16-3
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ def __init__(
4141
client=None,
4242
auto_refresh_url=None,
4343
auto_refresh_kwargs=None,
44+
auto_refresh_type="refresh_token",
4445
scope=None,
4546
redirect_uri=None,
4647
token=None,
@@ -67,6 +68,8 @@ def __init__(
6768
your access tokens.
6869
:auto_refresh_kwargs: Extra arguments to pass to the refresh token
6970
endpoint.
71+
:auto_refresh_type: Type of auto refresh method to use. Must be either
72+
"refresh_token" (default) or "access_token".
7073
:token_updater: Method with one argument, token, to be used to update
7174
your token database on automatic token refresh. If not
7275
set a TokenUpdated warning will be raised when a token
@@ -83,6 +86,7 @@ def __init__(
8386
self._state = state
8487
self.auto_refresh_url = auto_refresh_url
8588
self.auto_refresh_kwargs = auto_refresh_kwargs or {}
89+
self.auto_refresh_type = auto_refresh_type
8690
self.token_updater = token_updater
8791

8892
# Ensure that requests doesn't do any automatic auth. See #278.
@@ -499,9 +503,18 @@ def request(
499503
client_id,
500504
)
501505
auth = requests.auth.HTTPBasicAuth(client_id, client_secret)
502-
token = self.refresh_token(
503-
self.auto_refresh_url, auth=auth, **kwargs
504-
)
506+
if self.auto_refresh_type == "refresh_token":
507+
token = self.refresh_token(
508+
self.auto_refresh_url, auth=auth, **kwargs
509+
)
510+
elif self.auto_refresh_type == "access_token":
511+
token = self.fetch_token(
512+
self.auto_refresh_url,
513+
auth=auth,
514+
**dict(kwargs, **self.auto_refresh_kwargs),
515+
)
516+
else:
517+
raise RuntimeError("Unknown auto_refresh_type: %s" % self.auto_refresh_type)
505518
if self.token_updater:
506519
log.debug(
507520
"Updating token to %s using %s.", token, self.token_updater

0 commit comments

Comments
 (0)