]>
jfr.im git - z_archive/twitter.git/blob - twitter/auth.py
2 import urllib
.parse
as urllib_parse
3 from base64
import encodebytes
5 import urllib
as urllib_parse
6 from base64
import encodestring
as encodebytes
10 ABC for Authenticator objects.
13 def encode_params(self
, base_url
, method
, params
):
14 """Encodes parameters for a request suitable for including in a URL
15 or POST body. This method may also add new params to the request
16 if required by the authentication scheme in use."""
17 raise NotImplementedError()
19 def generate_headers(self
):
20 """Generates headers which should be added to the request if required
21 by the authentication scheme in use."""
22 raise NotImplementedError()
24 class UserPassAuth(Auth
):
26 Basic auth authentication using email/username and
29 def __init__(self
, username
, password
):
30 self
.username
= username
31 self
.password
= password
33 def encode_params(self
, base_url
, method
, params
):
34 # We could consider automatically converting unicode to utf8 strings
36 return urllib_parse
.urlencode(params
)
38 def generate_headers(self
):
39 return {b
"Authorization": b
"Basic " + encodebytes(
40 ("%s:%s" %(self
.username
, self
.password
))
41 .encode('utf8')).strip(b
'\n')
46 No authentication authenticator.
51 def encode_params(self
, base_url
, method
, params
):
52 return urllib_parse
.urlencode(params
)
54 def generate_headers(self
):