Skip to content

Commit 9db38f8

Browse files
Tristan Carellepture
authored andcommitted
OAuthRemoteApp: new access_token_headers parameter (lepture#222)
User can specify additional headers given to the access_token_url HTTP request. Rationale: This patch allows me to pass the HTTP 'Authentification' header to Bitbucket that requires basic access authentification for the access token method.
1 parent f4c8943 commit 9db38f8

File tree

1 file changed

+10
-2
lines changed

1 file changed

+10
-2
lines changed

flask_oauthlib/client.py

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -208,6 +208,8 @@ class OAuthRemoteApp(object):
208208
forward to the access token url
209209
:param access_token_method: the HTTP method that should be used for
210210
the access_token_url. Default is ``GET``
211+
:param access_token_headers: additonal headers that should be used for
212+
the access_token_url.
211213
:param content_type: force to parse the content with this content_type,
212214
usually used when the server didn't return the
213215
right content type.
@@ -229,6 +231,7 @@ def __init__(
229231
request_token_method=None,
230232
access_token_params=None,
231233
access_token_method=None,
234+
access_token_headers=None,
232235
content_type=None,
233236
app_key=None,
234237
encoding='utf-8',
@@ -246,6 +249,7 @@ def __init__(
246249
self._request_token_method = request_token_method
247250
self._access_token_params = access_token_params
248251
self._access_token_method = access_token_method
252+
self._access_token_headers = access_token_headers or {}
249253
self._content_type = content_type
250254
self._tokengetter = None
251255

@@ -469,7 +473,7 @@ def request(self, url, data=None, headers=None, format='urlencoded',
469473
else:
470474
data = None
471475
resp, content = self.http_request(
472-
uri, headers, data=data, method=method
476+
uri, headers, data=to_bytes(body, self.encoding), method=method
473477
)
474478
return OAuthResponse(resp, content, self.content_type)
475479

@@ -603,6 +607,7 @@ def handle_oauth1_response(self):
603607
self.expand_url(self.access_token_url),
604608
_encode(self.access_token_method)
605609
)
610+
headers.update(self._access_token_headers)
606611

607612
resp, content = self.http_request(
608613
uri, headers, to_bytes(data, self.encoding),
@@ -627,11 +632,13 @@ def handle_oauth2_response(self):
627632
}
628633
log.debug('Prepare oauth2 remote args %r', remote_args)
629634
remote_args.update(self.access_token_params)
635+
headers = copy(self._access_token_headers)
630636
if self.access_token_method == 'POST':
637+
headers.update({'Content-Type': 'application/x-www-form-urlencoded'})
631638
body = client.prepare_request_body(**remote_args)
632639
resp, content = self.http_request(
633640
self.expand_url(self.access_token_url),
634-
headers={'Content-Type': 'application/x-www-form-urlencoded'},
641+
headers=headers,
635642
data=to_bytes(body, self.encoding),
636643
method=self.access_token_method,
637644
)
@@ -641,6 +648,7 @@ def handle_oauth2_response(self):
641648
url += ('?' in url and '&' or '?') + qs
642649
resp, content = self.http_request(
643650
url,
651+
headers=headers,
644652
method=self.access_token_method,
645653
)
646654
else:

0 commit comments

Comments
 (0)