]>
jfr.im git - z_archive/twitter.git/blob - twitter/oauth.py
1 from __future__
import print_function
3 from twitter
.auth
import Auth
6 from random
import getrandbits
9 import urllib
.parse
as urllib_parse
11 import urllib2
as urllib_parse
19 def write_token_file(filename
, oauth_token
, oauth_token_secret
):
21 Write a token file to hold the oauth token and oauth token secret.
23 oauth_file
= open(filename
, 'w')
24 print(oauth_token
, file=oauth_file
)
25 print(oauth_token_secret
, file=oauth_file
)
28 def read_token_file(filename
):
30 Read a token file and return the oauth token and oauth token secret.
33 return f
.readline().strip(), f
.readline().strip()
38 An OAuth authenticator.
40 def __init__(self
, token
, token_secret
, consumer_key
, consumer_secret
):
42 Create the authenticator. If you are in the initial stages of
43 the OAuth dance and don't yet have a token or token_secret,
44 pass empty strings for these params.
47 self
.token_secret
= token_secret
48 self
.consumer_key
= consumer_key
49 self
.consumer_secret
= consumer_secret
51 def encode_params(self
, base_url
, method
, params
):
52 params
= params
.copy()
55 params
['oauth_token'] = self
.token
57 params
['oauth_consumer_key'] = self
.consumer_key
58 params
['oauth_signature_method'] = 'HMAC-SHA1'
59 params
['oauth_version'] = '1.0'
60 params
['oauth_timestamp'] = str(int(time()))
61 params
['oauth_nonce'] = str(getrandbits(64))
63 enc_params
= urlencode_noplus(sorted(params
.items()))
65 key
= self
.consumer_secret
+ "&" + urllib_parse
.quote(self
.token_secret
, '')
68 urllib_parse
.quote(i
, '') for i
in [method
.upper(), base_url
, enc_params
])
70 signature
= (base64
.b64encode(hmac
.new(
71 key
.encode('ascii'), message
.encode('ascii'), hashlib
.sha1
)
73 return enc_params
+ "&" + "oauth_signature=" + urllib_parse
.quote(signature
, '')
75 def generate_headers(self
):
78 # apparently contrary to the HTTP RFCs, spaces in arguments must be encoded as
79 # %20 rather than '+' when constructing an OAuth signature (and therefore
80 # also in the request itself.)
81 # So here is a specialized version which does exactly that.
82 def urlencode_noplus(query
):
83 if hasattr(query
,"items"):
85 query
= list(query
.items())
89 # and do unicode here while we are at it...
90 if isinstance(n
, str):
94 if isinstance(v
, str):
98 encoded_bits
.append("%s=%s" % (urllib_parse
.quote(n
, ""), urllib_parse
.quote(v
, "")))
99 return "&".join(encoded_bits
)