]>
jfr.im git - z_archive/twitter.git/blob - twitter/oauth.py
1 from twitter
.auth
import Auth
4 from random
import getrandbits
11 def write_token_file(filename
, oauth_token
, oauth_token_secret
):
13 Write a token file to hold the oauth token and oauth token secret.
15 oauth_file
= open(filename
, 'w')
16 print >> oauth_file
, oauth_token
17 print >> oauth_file
, oauth_token_secret
20 def read_token_file(filename
):
22 Read a token file and return the oauth token and oauth token secret.
25 return f
.readline().strip(), f
.readline().strip()
30 An OAuth authenticator.
32 def __init__(self
, token
, token_secret
, consumer_key
, consumer_secret
):
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.
39 self
.token_secret
= token_secret
40 self
.consumer_key
= consumer_key
41 self
.consumer_secret
= consumer_secret
43 def encode_params(self
, base_url
, method
, params
):
44 params
= params
.copy()
47 params
['oauth_token'] = self
.token
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))
55 enc_params
= urlencode_noplus(sorted(params
.iteritems()))
57 key
= self
.consumer_secret
+ "&" + urllib
.quote(self
.token_secret
, '')
60 urllib
.quote(i
, '') for i
in [method
.upper(), base_url
, enc_params
])
62 signature
= (base64
.b64encode(hmac
.new(
63 key
.encode('ascii'), message
.encode('ascii'), hashlib
.sha1
)
65 return enc_params
+ "&" + "oauth_signature=" + urllib
.quote(signature
, '')
67 def generate_headers(self
):
70 # apparently contrary to the HTTP RFCs, spaces in arguments must be encoded as
71 # %20 rather than '+' when constructing an OAuth signature (and therefore
72 # also in the request itself.)
73 # So here is a specialized version which does exactly that.
74 def urlencode_noplus(query
):
75 if hasattr(query
,"items"):
81 # and do unicode here while we are at it...
82 if isinstance(n
, unicode):
86 if isinstance(v
, unicode):
90 encoded_bits
.append("%s=%s" % (urllib
.quote(n
, ""), urllib
.quote(v
, "")))
91 return "&".join(encoded_bits
)