]>
jfr.im git - z_archive/twitter.git/blob - twitter/oauth.py
d2df7c121840ec20f02b8e08353db224c79636c4
1 from __future__
import print_function
3 from twitter
.auth
import Auth
6 from random
import getrandbits
9 import urllib
.parse
as urllib_parse
10 from urllib
.parse
import urlencode
13 import urllib2
as urllib_parse
14 from urllib
import urlencode
23 def write_token_file(filename
, oauth_token
, oauth_token_secret
):
25 Write a token file to hold the oauth token and oauth token secret.
27 oauth_file
= open(filename
, 'w')
28 print(oauth_token
, file=oauth_file
)
29 print(oauth_token_secret
, file=oauth_file
)
32 def read_token_file(filename
):
34 Read a token file and return the oauth token and oauth token secret.
37 return f
.readline().strip(), f
.readline().strip()
42 An OAuth authenticator.
44 def __init__(self
, token
, token_secret
, consumer_key
, consumer_secret
):
46 Create the authenticator. If you are in the initial stages of
47 the OAuth dance and don't yet have a token or token_secret,
48 pass empty strings for these params.
51 self
.token_secret
= token_secret
52 self
.consumer_key
= consumer_key
53 self
.consumer_secret
= consumer_secret
55 def encode_params(self
, base_url
, method
, params
):
56 params
= params
.copy()
59 params
['oauth_token'] = self
.token
61 params
['oauth_consumer_key'] = self
.consumer_key
62 params
['oauth_signature_method'] = 'HMAC-SHA1'
63 params
['oauth_version'] = '1.0'
64 params
['oauth_timestamp'] = str(int(time()))
65 params
['oauth_nonce'] = str(getrandbits(64))
67 enc_params
= urlencode_noplus(sorted(params
.items()))
69 key
= self
.consumer_secret
+ "&" + urllib_parse
.quote(self
.token_secret
, '')
72 urllib_parse
.quote(i
, '') for i
in [method
.upper(), base_url
, enc_params
])
74 signature
= (base64
.b64encode(hmac
.new(
75 key
.encode('ascii'), message
.encode('ascii'), hashlib
.sha1
)
77 return enc_params
+ "&" + "oauth_signature=" + urllib_parse
.quote(signature
, '')
79 def generate_headers(self
):
82 # apparently contrary to the HTTP RFCs, spaces in arguments must be encoded as
83 # %20 rather than '+' when constructing an OAuth signature (and therefore
84 # also in the request itself.)
85 # So here is a specialized version which does exactly that.
86 def urlencode_noplus(query
):
90 if type(k
) is unicode: k
= k
.encode('utf-8')
91 if type(v
) is unicode: v
= v
.encode('utf-8')
92 new_query
.append((k
, v
))
94 return urlencode(query
).replace("+", "%20")