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

Move auth calculation into refresh_token method #340

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
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
21 changes: 11 additions & 10 deletions requests_oauthlib/oauth2_session.py
Original file line number Diff line number Diff line change
Expand Up @@ -282,6 +282,15 @@ def refresh_token(self, token_url, refresh_token=None, body='', auth=None,
log.debug('Adding auto refresh key word arguments %s.',
self.auto_refresh_kwargs)
kwargs.update(self.auto_refresh_kwargs)

auth = auth or kwargs.get('auth')
client_id = kwargs.get('client_id')
client_secret = kwargs.get('client_secret', '')

if client_id and (auth is None):
log.debug('Encoding client_id "%s" with client_secret as Basic auth credentials.', client_id)
auth = requests.auth.HTTPBasicAuth(client_id, client_secret)

body = self._client.prepare_refresh_body(body=body,
refresh_token=refresh_token, scope=self.scope, **kwargs)
log.debug('Prepared refresh token request body %s', body)
Expand Down Expand Up @@ -312,8 +321,7 @@ def refresh_token(self, token_url, refresh_token=None, body='', auth=None,
self.token['refresh_token'] = refresh_token
return self.token

def request(self, method, url, data=None, headers=None, withhold_token=False,
client_id=None, client_secret=None, **kwargs):
def request(self, method, url, data=None, headers=None, withhold_token=False, **kwargs):
"""Intercept all requests and add the OAuth 2 token if present."""
if not is_secure_transport(url):
raise InsecureTransportError()
Expand All @@ -334,14 +342,7 @@ def request(self, method, url, data=None, headers=None, withhold_token=False,
log.debug('Auto refresh is set, attempting to refresh at %s.',
self.auto_refresh_url)

# We mustn't pass auth twice.
auth = kwargs.pop('auth', None)
if client_id and client_secret and (auth is None):
log.debug('Encoding client_id "%s" with client_secret as Basic auth credentials.', client_id)
auth = requests.auth.HTTPBasicAuth(client_id, client_secret)
token = self.refresh_token(
self.auto_refresh_url, auth=auth, **kwargs
)
token = self.refresh_token(self.auto_refresh_url, **kwargs)
if self.token_updater:
log.debug('Updating token to %s using %s.',
token, self.token_updater)
Expand Down