X-Git-Url: https://jfr.im/git/z_archive/twitter.git/blobdiff_plain/9376cfa693173f3dea378c32c1d24668eece59ca..dcece3a61cb23ca20f55e0f0b2572eb6b75e3941:/twitter/auth.py diff --git a/twitter/auth.py b/twitter/auth.py index 9aad691..77633ff 100644 --- a/twitter/auth.py +++ b/twitter/auth.py @@ -1,5 +1,9 @@ -import urllib.request, urllib.parse, urllib.error -from base64 import encodestring +try: + import urllib.parse as urllib_parse + from base64 import encodebytes +except ImportError: + import urllib as urllib_parse + from base64 import encodestring as encodebytes class Auth(object): """ @@ -17,6 +21,25 @@ class Auth(object): by the authentication scheme in use.""" raise NotImplementedError() +class UserPassAuth(Auth): + """ + Basic auth authentication using email/username and + password. Deprecated. + """ + def __init__(self, username, password): + self.username = username + self.password = password + + def encode_params(self, base_url, method, params): + # We could consider automatically converting unicode to utf8 strings + # before encoding... + return urllib_parse.urlencode(params) + + def generate_headers(self): + return {b"Authorization": b"Basic " + encodebytes( + ("%s:%s" %(self.username, self.password)) + .encode('utf8')).strip(b'\n') + } class NoAuth(Auth): """ @@ -26,7 +49,7 @@ class NoAuth(Auth): pass def encode_params(self, base_url, method, params): - return urllib.parse.urlencode(params) + return urllib_parse.urlencode(params) def generate_headers(self): return {}