Permalink
Browse files

Added Client.request(..., force_auth_header=False) to force sending A…

…uthorization header

This is required by LinkedIn API at this time.
  • Loading branch information...
1 parent 53780ee commit 82a05f96878f187f67c1af44befc1bec562e5c1f @brosner brosner committed Jan 15, 2010
Showing with 20 additions and 6 deletions.
  1. +20 −6 oauth2/__init__.py
View
@@ -309,11 +309,14 @@ def to_header(self, realm=''):
def to_postdata(self):
"""Serialize as post data for a POST request."""
+ return self.encode_postdata(self)
+
+ def encode_postdata(self, data):
# tell urlencode to deal with sequence values and map them correctly
# to resulting querystring. for example self["k"] = ["v1", "v2"] will
# result in 'k=v1&k=v2' and not k=%5B%27v1%27%2C+%27v2%27%5D
- return urllib.urlencode(self, True)
-
+ return urllib.urlencode(data, True)
+
def to_url(self):
"""Serialize as a URL for a GET request."""
return '%s?%s' % (self.url, self.to_postdata())
@@ -574,7 +577,8 @@ def set_signature_method(self, method):
self.method = method
def request(self, uri, method="GET", body=None, headers=None,
- redirections=httplib2.DEFAULT_MAX_REDIRECTS, connection_type=None):
+ redirections=httplib2.DEFAULT_MAX_REDIRECTS, connection_type=None,
+ force_auth_header=False):
if not isinstance(headers, dict):
headers = {}
@@ -592,13 +596,23 @@ def request(self, uri, method="GET", body=None, headers=None,
req.sign_request(self.method, self.consumer, self.token)
+ if force_auth_header:
+ # ensure we always send Authorization
+ headers.update(req.to_header())
+
if method == "POST":
- body = req.to_postdata()
+ if not force_auth_header:
+ body = req.to_postdata()
+ else:
+ body = req.encode_postdata(req.get_nonoauth_parameters())
headers['Content-Type'] = 'application/x-www-form-urlencoded'
elif method == "GET":
- uri = req.to_url()
+ if not force_auth_header:
+ uri = req.to_url()
else:
- headers.update(req.to_header())
+ if not force_auth_header:
+ # don't call update twice.
+ headers.update(req.to_header())
return httplib2.Http.request(self, uri, method=method, body=body,
headers=headers, redirections=redirections,

0 comments on commit 82a05f9

Please sign in to comment.