Skip to content

Commit 800976f

Browse files
Add force_querystring argument to fetch_token() method
1 parent c80b2b6 commit 800976f

File tree

3 files changed

+39
-32
lines changed

3 files changed

+39
-32
lines changed

HISTORY.rst

+1
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ UNRELEASED
55
++++++++++
66

77
- Instagram compliance fix
8+
- Added ``force_querystring`` argument to fetch_token() method on OAuth2Session
89

910
v1.2.0 (14 January 2019)
1011
++++++++++++++++++++++++

requests_oauthlib/oauth2_session.py

+19-20
Original file line numberDiff line numberDiff line change
@@ -182,6 +182,7 @@ def fetch_token(
182182
username=None,
183183
password=None,
184184
method="POST",
185+
force_querystring=False,
185186
timeout=None,
186187
headers=None,
187188
verify=True,
@@ -212,6 +213,8 @@ def fetch_token(
212213
:param method: The HTTP method used to make the request. Defaults
213214
to POST, but may also be GET. Other methods should
214215
be added as needed.
216+
:param force_querystring: If True, force the request body to be sent
217+
in the querystring instead.
215218
:param timeout: Timeout of the request in seconds.
216219
:param headers: Dict to default request headers with.
217220
:param verify: Verify SSL certificate.
@@ -320,33 +323,29 @@ def fetch_token(
320323
"Content-Type": "application/x-www-form-urlencoded;charset=UTF-8",
321324
}
322325
self.token = {}
326+
request_kwargs = {}
323327
if method.upper() == "POST":
324-
r = self.post(
325-
token_url,
326-
data=dict(urldecode(body)),
327-
timeout=timeout,
328-
headers=headers,
329-
auth=auth,
330-
verify=verify,
331-
proxies=proxies,
328+
request_kwargs["params" if force_querystring else "data"] = dict(
329+
urldecode(body)
332330
)
333-
log.debug("Prepared fetch token request body %s", body)
334331
elif method.upper() == "GET":
335-
# if method is not 'POST', switch body to querystring and GET
336-
r = self.get(
337-
token_url,
338-
params=dict(urldecode(body)),
339-
timeout=timeout,
340-
headers=headers,
341-
auth=auth,
342-
verify=verify,
343-
proxies=proxies,
344-
)
345-
log.debug("Prepared fetch token request querystring %s", body)
332+
request_kwargs["params"] = dict(urldecode(body))
346333
else:
347334
raise ValueError("The method kwarg must be POST or GET.")
348335

336+
r = self.request(
337+
method=method,
338+
url=token_url,
339+
timeout=timeout,
340+
headers=headers,
341+
auth=auth,
342+
verify=verify,
343+
proxies=proxies,
344+
**request_kwargs
345+
)
346+
349347
log.debug("Request to fetch token completed with status %s.", r.status_code)
348+
log.debug("Request url was %s", r.request.url)
350349
log.debug("Request headers were %s", r.request.headers)
351350
log.debug("Request body was %s", r.request.body)
352351
log.debug("Response headers were %s and content %s.", r.headers, r.text)

tests/test_oauth2_session.py

+19-12
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525

2626

2727
fake_time = time.time()
28+
CODE = "asdf345xdf"
2829

2930

3031
def fake_token(token):
@@ -51,9 +52,7 @@ def setUp(self):
5152
self.client_secret = "someclientsecret"
5253
self.user_username = "user_username"
5354
self.user_password = "user_password"
54-
self.client_WebApplication = WebApplicationClient(
55-
self.client_id, code="asdf345xdf"
56-
)
55+
self.client_WebApplication = WebApplicationClient(self.client_id, code=CODE)
5756
self.client_LegacyApplication = LegacyApplicationClient(self.client_id)
5857
self.client_BackendApplication = BackendApplicationClient(self.client_id)
5958
self.client_MobileApplication = MobileApplicationClient(self.client_id)
@@ -291,7 +290,7 @@ def fake_send(r, **kwargs):
291290
_fetch_history[2][2], expected_auth_header
292291
) # ensure a Basic Authorization header
293292

294-
# scneario 4 - send in a username/password combo
293+
# scenario 4 - send in a username/password combo
295294
# this should send the `client_id` in the headers, like scenario 1
296295
self.assertEqual(
297296
sess.fetch_token(
@@ -312,31 +311,39 @@ def fake_send(r, **kwargs):
312311
self.assertIn("username=%s" % self.user_username, _fetch_history[3][1])
313312
self.assertIn("password=%s" % self.user_password, _fetch_history[3][1])
314313

314+
# scenario 5 - send data in `params` and not in `data` for providers
315+
# that expect data in URL
316+
self.assertEqual(
317+
sess.fetch_token(url, client_secret="somesecret", force_querystring=True),
318+
self.token,
319+
)
320+
self.assertIn("code=%s" % CODE, _fetch_history[4][0])
321+
315322
# some quick tests for valid ways of supporting `client_secret`
316323

317324
# scenario 2b - force the `client_id` into the body; but the `client_secret` is `None`
318325
self.assertEqual(
319326
sess.fetch_token(url, client_secret=None, include_client_id=True),
320327
self.token,
321328
)
322-
self.assertEqual(len(_fetch_history), 5)
323-
self.assertIn("client_id=%s" % self.client_id, _fetch_history[4][1])
329+
self.assertEqual(len(_fetch_history), 6)
330+
self.assertIn("client_id=%s" % self.client_id, _fetch_history[5][1])
324331
self.assertNotIn(
325-
"client_secret", _fetch_history[4][1]
332+
"client_secret=", _fetch_history[5][1]
326333
) # no `client_secret` in the body
327334
self.assertEqual(
328-
_fetch_history[4][2], None
335+
_fetch_history[5][2], None
329336
) # ensure NO Basic Authorization header
330337

331338
# scenario 2c - force the `client_id` into the body; but the `client_secret` is an empty string
332339
self.assertEqual(
333340
sess.fetch_token(url, client_secret="", include_client_id=True), self.token
334341
)
335-
self.assertEqual(len(_fetch_history), 6)
336-
self.assertIn("client_id=%s" % self.client_id, _fetch_history[5][1])
337-
self.assertIn("client_secret=", _fetch_history[5][1])
342+
self.assertEqual(len(_fetch_history), 7)
343+
self.assertIn("client_id=%s" % self.client_id, _fetch_history[6][1])
344+
self.assertIn("client_secret=", _fetch_history[6][1])
338345
self.assertEqual(
339-
_fetch_history[5][2], None
346+
_fetch_history[6][2], None
340347
) # ensure NO Basic Authorization header
341348

342349
def test_cleans_previous_token_before_fetching_new_one(self):

0 commit comments

Comments
 (0)