9
9
__author__ = "Claudio Sanches @ WooThemes"
10
10
__license__ = "MIT"
11
11
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
19
28
20
29
21
30
class OAuth (object ):
@@ -40,13 +49,16 @@ def get_oauth_url(self):
40
49
url = self .url
41
50
42
51
params ["oauth_consumer_key" ] = self .consumer_key
43
- params ["oauth_timestamp" ] <
10000
span class=pl-c1>= 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 ()
46
58
params ["oauth_signature_method" ] = "HMAC-SHA256"
47
59
params ["oauth_signature" ] = self .generate_oauth_signature (params , url )
48
60
49
- query_string = urllib . urlencode (params )
61
+ query_string = urlencode (params )
50
62
51
63
return "%s?%s" % (url , query_string )
52
64
@@ -55,9 +67,9 @@ def generate_oauth_signature(self, params, url):
55
67
if "oauth_signature" in params .keys ():
56
68
del params ["oauth_signature" ]
57
69
58
- base_request_uri = urllib . quote (url , "" )
70
+ base_request_uri = quote (url , "" )
59
71
params = self .normalize_parameters (params )
60
- params = collections . OrderedDict (sorted (params .items ()))
72
+ params = OrderedDict (sorted (params .items ()))
61
73
query_params = ["{param_key}%3D{param_value}" .format (param_key = key , param_value = value )
62
74
for key , value in params .items ()]
63
75
@@ -68,12 +80,13 @@ def generate_oauth_signature(self, params, url):
68
80
if self .version == "v3" :
69
81
consumer_secret += "&"
70
82
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 ()
75
88
76
- return hash_signature . encode ( "base64 " ).replace ("\n " , "" )
89
+ return b64encode ( hash_signature ). decode ( "utf-8 " ).replace ("\n " , "" )
77
90
78
91
@staticmethod
79
92
def normalize_parameters (params ):
@@ -82,8 +95,13 @@ def normalize_parameters(params):
82
95
normalized_parameters = {}
83
96
84
97
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 ):
87
105
return val
88
106
elif isinstance (val , bool ):
89
107
return "1" if val else ""
@@ -96,8 +114,8 @@ def get_value_like_as_php(val):
96
114
97
115
for key , value in params .items ():
98
116
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" )
101
119
normalized_parameters [key ] = value
102
120
103
121
return normalized_parameters
0 commit comments