X-Git-Url: https://jfr.im/git/z_archive/twitter.git/blobdiff_plain/1cc9ab0b8a70466bf8cf684fdab28aad342696c8..dcece3a61cb23ca20f55e0f0b2572eb6b75e3941:/twitter/auth.py diff --git a/twitter/auth.py b/twitter/auth.py index f22b088..77633ff 100644 --- a/twitter/auth.py +++ b/twitter/auth.py @@ -1,5 +1,9 @@ -import urllib -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): """ @@ -10,13 +14,12 @@ class Auth(object): """Encodes parameters for a request suitable for including in a URL or POST body. This method may also add new params to the request if required by the authentication scheme in use.""" - raise NotImplementedError + raise NotImplementedError() def generate_headers(self): """Generates headers which should be added to the request if required by the authentication scheme in use.""" - raise NotImplementedError - + raise NotImplementedError() class UserPassAuth(Auth): """ @@ -30,18 +33,23 @@ class UserPassAuth(Auth): def encode_params(self, base_url, method, params): # We could consider automatically converting unicode to utf8 strings # before encoding... - return urllib.urlencode(params) + return urllib_parse.urlencode(params) def generate_headers(self): - return {"Authorization": "Basic " + encodestring("%s:%s" %( - self.username, self.password)).strip('\n')} + return {b"Authorization": b"Basic " + encodebytes( + ("%s:%s" %(self.username, self.password)) + .encode('utf8')).strip(b'\n') + } -class NoAuth(UserPassAuth): +class NoAuth(Auth): """ No authentication authenticator. """ def __init__(self): pass + def encode_params(self, base_url, method, params): + return urllib_parse.urlencode(params) + def generate_headers(self): return {}