Skip to content

Commit 216a377

Browse files
committed
Added a few fixes to get python-oauth2 working on Python 2.4.
1 parent 5cd5736 commit 216a377

File tree

1 file changed

+21
-8
lines changed

1 file changed

+21
-8
lines changed

oauth2/__init__.py

Lines changed: 21 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,6 @@
2929
import hmac
3030
import binascii
3131
import httplib2
32-
from types import ListType
3332

3433
try:
3534
from urlparse import parse_qs, parse_qsl
@@ -328,9 +327,17 @@ def get_parameter(self, parameter):
328327

329328
def get_normalized_parameters(self):
330329
"""Return a string that contains the parameters that must be signed."""
331-
# 1.0a/9.1.1 states that kvp must be sorted by key, then by value
332-
items = [(k, v if type(v) != ListType else sorted(v)) for k,v in sorted(self.items()) if k != 'oauth_signature']
333-
encoded_str = urllib.urlencode(items, True)
330+
items = []
331+
for key, value in self.iteritems():
332+
if key == 'oauth_signature':
333+
continue
334+
# 1.0a/9.1.1 states that kvp must be sorted by key, then by value,
335+
# so we unpack sequence values into multiple items for sorting.
336+
if hasattr(value, '__iter__'):
337+
items.extend((key, item) for item in value)
338+
else:
339+
items.append((key, value))
340+
encoded_str = urllib.urlencode(sorted(items))
334341
# Encode signature parameters per Oauth Core 1.0 protocol
335342
# spec draft 7, section 3.6
336343
# (http://tools.ietf.org/html/draft-hammer-oauth-07#section-3.6)
@@ -585,7 +592,13 @@ def request(self, uri, method="GET", body=None, headers=None,
585592
parameters = dict(parse_qsl(body))
586593
elif method == "GET":
587594
parsed = urlparse.urlparse(uri)
588-
parameters = parse_qsl(parsed.query)
595+
596+
try:
597+
query = parsed.query
598+
except AttributeError:
599+
query = parsed[4]
600+
601+
parameters = parse_qsl(query)
589602
else:
590603
parameters = None
591604

@@ -665,11 +678,11 @@ def sign(self, request, consumer, token):
665678

666679
# HMAC object.
667680
try:
668-
import hashlib # 2.5
669-
hashed = hmac.new(key, raw, hashlib.sha1)
681+
import hashlib.sha1 as sha # 2.5
670682
except ImportError:
671683
import sha # Deprecated
672-
hashed = hmac.new(key, raw, sha)
684+
685+
hashed = hmac.new(key, raw, sha)
673686

674687
# Calculate the digest base 64.
675688
return binascii.b2a_base64(hashed.digest())[:-1]

0 commit comments

Comments
 (0)