-
Notifications
You must be signed in to change notification settings - Fork 0
/
util_oauth.py
61 lines (44 loc) · 1.66 KB
/
util_oauth.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# OAUTH signature creator
import hashlib
import hmac
import urllib
from random import getrandbits
from constants import TWITTER_CONSUMER_KEY_SECRET
class OAuthSignature():
# Twitter API url
url = ""
# User secret keys
secrets = {
'consumer_secret': "",
'token_secret': ""
}
def generate(self, params):
"""
Generate Twitter signature
"""
# Step 1. Collecting parameters
params_str = '&'.join(
[('%s=%s' % (self.encode(str(k)), self.encode(str(params[k])))) for k in sorted(params)])
# Step 2. Creating the signature base string
# Join the entire message together per the OAuth specification.
message = "&".join(
[self.encode("POST"), self.encode(self.url), self.encode(params_str)])
# Step 3. Getting a signing key
# consumer secret key
cSecret = self.encode(self.secrets.get('consumer_secret'))
# consumer key
tSecret = self.encode(self.secrets.get('token_secret'))
# The signing key is simply the percent encoded consumer secret,
# followed by an ampersand character,
# followed by the percent encoded token secret
key = "%s&%s" % (cSecret, tSecret)
# Step 4. Calculating the signature
# Create a HMAC-SHA1 signature of the message.
signature = hmac.new(b'key', message, hashlib.sha1).digest()
digestBase64 = signature.encode("base64").rstrip('\n')
return digestBase64
def nonce(self):
""" Generate random nonce value"""
return str(getrandbits(64))
def encode(self, text):
return urllib.parse.quote(str(text), "")