]>
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
11 ABC for Authenticator objects.
14 def encode_params(self
, base_url
, method
, params
):
15 """Encodes parameters for a request suitable for including in a URL
16 or POST body. This method may also add new params to the request
17 if required by the authentication scheme in use."""
18 raise NotImplementedError()
20 def generate_headers(self
):
21 """Generates headers which should be added to the request if required
22 by the authentication scheme in use."""
23 raise NotImplementedError()
26 class UserPassAuth(Auth
):
28 Basic auth authentication using email/username and
31 def __init__(self
, username
, password
):
32 self
.username
= username
33 self
.password
= password
35 def encode_params(self
, base_url
, method
, params
):
36 # We could consider automatically converting unicode to utf8 strings
38 return urllib_parse
.urlencode(params
)
40 def generate_headers(self
):
41 return {b
"Authorization": b
"Basic " + encodebytes(
42 ("%s:%s" %(self
.username
, self
.password
))
43 .encode('utf8')).strip(b
'\n')
49 No authentication authenticator.
54 def encode_params(self
, base_url
, method
, params
):
55 return urllib_parse
.urlencode(params
)
57 def generate_headers(self
):
61 class MissingCredentialsError(Exception):