]> jfr.im git - z_archive/twitter.git/blob - twitter/oauth.py
Make search work again
[z_archive/twitter.git] / twitter / oauth.py
1 from twitter.auth import Auth
2
3 from time import time
4 from random import getrandbits
5 from time import time
6 import urllib
7 import hashlib
8 import hmac
9
10
11 def write_token_file(filename, oauth_token, oauth_token_secret):
12 """
13 Write a token file to hold the oauth token and oauth token secret.
14 """
15 oauth_file = open(filename, 'w')
16 print >> oauth_file, oauth_token
17 print >> oauth_file, oauth_token_secret
18 oauth_file.close()
19
20 def read_token_file(filename):
21 """
22 Read a token file and return the oauth token and oauth token secret.
23 """
24 f = open(filename)
25 return f.readline().strip(), f.readline().strip()
26
27
28 class OAuth(Auth):
29 """
30 An OAuth authenticator.
31 """
32 def __init__(self, token, token_secret, consumer_key, consumer_secret):
33 """
34 Create the authenticator. If you are in the initial stages of
35 the OAuth dance and don't yet have a token or token_secret,
36 pass empty strings for these params.
37 """
38 self.token = token
39 self.token_secret = token_secret
40 self.consumer_key = consumer_key
41 self.consumer_secret = consumer_secret
42
43 def encode_params(self, base_url, method, params):
44 params = params.copy()
45
46 if self.token:
47 params['oauth_token'] = self.token
48
49 params['oauth_consumer_key'] = self.consumer_key
50 params['oauth_signature_method'] = 'HMAC-SHA1'
51 params['oauth_version'] = '1.0'
52 params['oauth_timestamp'] = str(int(time()))
53 params['oauth_nonce'] = str(getrandbits(64))
54
55 enc_params = urlencode_noplus(sorted(params.iteritems()))
56
57 key = self.consumer_secret + "&" + urllib.quote(self.token_secret, '')
58
59 message = '&'.join(
60 urllib.quote(i, '') for i in [method.upper(), base_url, enc_params])
61
62 signature = hmac.new(
63 key, message, hashlib.sha1).digest().encode('base64')[:-1]
64 return enc_params + "&" + "oauth_signature=" + urllib.quote(signature, '')
65
66 def generate_headers(self):
67 return {}
68
69 # apparently contrary to the HTTP RFCs, spaces in arguments must be encoded as
70 # %20 rather than '+' when constructing an OAuth signature (and therefore
71 # also in the request itself.)
72 # So here is a specialized version which does exactly that.
73 def urlencode_noplus(query):
74 if hasattr(query,"items"):
75 # mapping objects
76 query = query.items()
77
78 encoded_bits = []
79 for n, v in query:
80 # and do unicode here while we are at it...
81 if isinstance(n, unicode):
82 n = n.encode('utf-8')
83 else:
84 n = str(n)
85 if isinstance(v, unicode):
86 v = v.encode('utf-8')
87 else:
88 v = str(v)
89 encoded_bits.append("%s=%s" % (urllib.quote(n, ""), urllib.quote(v, "")))
90 return "&".join(encoded_bits)