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

fetch_token option for params #327

Open
wants to merge 1 commit 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
20 changes: 15 additions & 5 deletions requests_oauthlib/oauth2_session.py
Original file line number Diff line number Diff line change
@@ -154,7 +154,7 @@ def authorization_url(self, url, state=None, **kwargs):

def fetch_token(self, token_url, code=None, authorization_response=None,
body='', auth=None, username=None, password=None, method='POST',
timeout=None, headers=None, verify=True, proxies=None, **kwargs):
timeout=None, headers=None, verify=True, proxies=None, post_params=None, **kwargs):
"""Generic method for fetching an access token from the token endpoint.

If you are using the MobileApplicationClient you will want to use
@@ -177,6 +177,7 @@ def fetch_token(self, token_url, code=None, authorization_response=None,
:param timeout: Timeout of the request in seconds.
:param verify: Verify SSL certificate.
:param kwargs: Extra parameters to include in the token request.
:param post_params: If True, sends body as url query string.
:return: A token dict
"""
if not is_secure_transport(token_url):
@@ -215,11 +216,16 @@ def fetch_token(self, token_url, code=None, authorization_response=None,
'Content-Type': 'application/x-www-form-urlencoded;charset=UTF-8',
}
self.token = {}
if method.upper() == 'POST':
if method.upper() == 'POST' and not post_params:
r = self.post(token_url, data=dict(urldecode(body)),
timeout=timeout, headers=headers, auth=auth,
verify=verify, proxies=proxies)
log.debug('Prepared fetch token request body %s', body)
elif method.upper() == 'POST':
r = self.post(token_url, params=dict(urldecode(body)),
timeout=timeout, headers=headers, auth=auth,
verify=verify, proxies=proxies)
log.debug('Prepared fetch token request body %s', body)
elif method.upper() == 'GET':
# if method is not 'POST', switch body to querystring and GET
r = self.get(token_url, params=dict(urldecode(body)),
@@ -258,7 +264,7 @@ def token_from_fragment(self, authorization_response):
return self.token

def refresh_token(self, token_url, refresh_token=None, body='', auth=None,
timeout=None, headers=None, verify=True, proxies=None, **kwargs):
timeout=None, headers=None, verify=True, proxies=None, post_params=None, **kwargs):
"""Fetch a new access token using a refresh token.

:param token_url: The token endpoint, must be HTTPS.
@@ -269,6 +275,7 @@ def refresh_token(self, token_url, refresh_token=None, body='', auth=None,
:param timeout: Timeout of the request in seconds.
:param verify: Verify SSL certificate.
:param kwargs: Extra parameters to include in the token request.
:param post_params: If True, sends body as url query string.
:return: A token dict
"""
if not token_url:
@@ -293,8 +300,11 @@ def refresh_token(self, token_url, refresh_token=None, body='', auth=None,
'application/x-www-form-urlencoded;charset=UTF-8'
),
}

r = self.post(token_url, data=dict(urldecode(body)), auth=auth,
if not post_params:
r = self.post(token_url, data=dict(urldecode(body)), auth=auth,
timeout=timeout, headers=headers, verify=verify, withhold_token=True, proxies=proxies)
else:
r = self.post(token_url, data=params(urldecode(body)), auth=auth,
timeout=timeout, headers=headers, verify=verify, withhold_token=True, proxies=proxies)
log.debug('Request to refresh token completed with status %s.',
r.status_code)