Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for auto-refreshing token without refresh token #394

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 16 additions & 3 deletions requests_oauthlib/oauth2_session.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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
Expand All @@ -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.
Expand Down Expand Up @@ -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
Expand Down