99__author__ = "Claudio Sanches @ WooThemes"
1010__license__ = "MIT"
1111
12- import urlparse
13- import time
14- import random
15- import hmac
16- import urllib
17- import hashlib
18- import collections
12+ from time import time
13+ from random import randint
14+ from hmac import new as HMAC
15+ from hashlib import sha1 , sha256
16+ from collections import OrderedDict
17+ from base64 import b64encode
18+
19+ try :
20+ from urllib .parse import urlparse
21+ except ImportError :
22+ from urlparse import urlparse
23+
24+ try :
25+ from urllib import urlencode , quote , unquote
26+ except ImportError :
27+ from urllib .parse import urlencode , quote , unquote
1928
2029
2130class OAuth (object ):
@@ -40,13 +49,16 @@ def get_oauth_url(self):
4049 url = self .url
4150
4251 params ["oauth_consumer_key" ] = self .consumer_key
43- params ["oauth_timestamp" ] = int (time .time ())
44- params ["oauth_nonce" ] = hmac .new (
45- str (time .time () + random .randint (0 , 99999 )), "SHA1" ).hexdigest ()
52+ params ["oauth_timestamp" ] = int (time ())
53+ params ["oauth_nonce" ] = HMAC (
54+ str (time () + randint (0 , 99999 )).encode (),
55+ "secret" .encode (),
56+ sha1
57+ ).hexdigest ()
4658 params ["oauth_signature_method" ] = "HMAC-SHA256"
4759 params ["oauth_signature" ] = self .generate_oauth_signature (params , url )
4860
49- query_string = urllib . urlencode (params )
61+ query_string = urlencode (params )
5062
5163 return "%s?%s" % (url , query_string )
5264
@@ -55,9 +67,9 @@ def generate_oauth_signature(self, params, url):
5567 if "oauth_signature" in params .keys ():
5668 del params ["oauth_signature" ]
5769
58- base_request_uri = urllib . quote (url , "" )
70+ base_request_uri = quote (url , "" )
5971 params = self .normalize_parameters (params )
60- params = collections . OrderedDict (sorted (params .items ()))
72+ params = OrderedDict (sorted (params .items ()))
6173 query_params = ["{param_key}%3D{param_value}" .format (param_key = key , param_value = value )
6274 for key , value in params .items ()]
6375
@@ -68,12 +80,13 @@ def generate_oauth_signature(self, params, url):
6880 if self .version == "v3" :
6981 consumer_secret += "&"
7082
71- hash_signature = hmac .new (
72- consumer_secret ,
73- str (string_to_sign ),
74- getattr (hashlib , "sha256" )).digest ()
83+ hash_signature = HMAC (
84+ consumer_secret .encode (),
85+ str (string_to_sign ).encode (),
86+ sha256
87+ ).digest ()
7588
76- return hash_signature . encode ( "base64 " ).replace ("\n " , "" )
89+ return b64encode ( hash_signature ). decode ( "utf-8 " ).replace ("\n " , "" )
7790
7891 @staticmethod
7992 def normalize_parameters (params ):
@@ -82,8 +95,13 @@ def normalize_parameters(params):
8295 normalized_parameters = {}
8396
8497 def get_value_like_as_php (val ):
85- """ Prepare value for urllib.quote """
86- if isinstance (val , basestring ):
98+ """ Prepare value for quote """
99+ try :
100+ base = basestring
101+ except NameError :
102+ base = (str , bytes )
103+
104+ if isinstance (val , base ):
87105 return val
88106 elif isinstance (val , bool ):
89107 return "1" if val else ""
@@ -96,8 +114,8 @@ def get_value_like_as_php(val):
96114
97115 for key , value in params .items ():
98116 value = get_value_like_as_php (value )
99- key = urllib . quote (urllib . unquote (str (key ))).replace ("%" , "%25" )
100- value = urllib . quote (urllib . unquote (str (value ))).replace ("%" , "%25" )
117+ key = quote (unquote (str (key ))).replace ("%" , "%25" )
118+ value = quote (unquote (str (value ))).replace ("%" , "%25" )
101119 normalized_parameters [key ] = value
102120
103121 return normalized_parameters
0 commit comments