From: Josh Roesslein Date: Thu, 30 Jul 2009 22:22:04 +0000 (-0500) Subject: Finished up oauth handler. Tested and working. X-Git-Url: https://vcs.fsf.org/?a=commitdiff_plain;h=cfd073c8b69b73c14954fd9734b2be4ce12dc3fe;p=tweepy.git Finished up oauth handler. Tested and working. --- diff --git a/tweepy/auth.py b/tweepy/auth.py index db7db97..97be9f5 100644 --- a/tweepy/auth.py +++ b/tweepy/auth.py @@ -10,7 +10,7 @@ from error import TweepError class AuthHandler(object): - def apply_auth(self, headers): + def apply_auth(self, url, method, headers, parameters): """Apply authentication headers to request""" raise NotImplemented @@ -19,7 +19,7 @@ class BasicAuthHandler(AuthHandler): def __init__(self, username, password): self._b64up = base64.b64encode('%s:%s' % (username, password)) - def apply_auth(self, headers): + def apply_auth(self, url, method, headers, parameters): headers['Authorization'] = 'Basic %s' % self._b64up """OAuth authentication handler""" @@ -35,6 +35,12 @@ class OAuthHandler(AuthHandler): self.request_token = None self.access_token = None + def apply_auth(self, url, method, headers, parameters): + request = oauth.OAuthRequest.from_consumer_and_token(self._consumer, + http_url=url, http_method=method, token=self.access_token, parameters=parameters) + request.sign_request(self._sigmethod, self._consumer, self.access_token) + headers.update(request.to_header()) + def _get_request_token(self): try: request = oauth.OAuthRequest.from_consumer_and_token(self._consumer, http_url = self.REQUEST_TOKEN_URL) @@ -52,17 +58,18 @@ class OAuthHandler(AuthHandler): # build auth request and return as url request = oauth.OAuthRequest.from_token_and_callback( - token=token, callback=callback, http_url=self.AUTHORIZATION_URL) + token=self.request_token, callback=callback, http_url=self.AUTHORIZATION_URL) return request.to_url() except Exception, e: raise TweepError(e) - def get_access_token(self): + def get_access_token(self, verifier): try: # build request request = oauth.OAuthRequest.from_consumer_and_token(self._consumer, token=self.request_token, http_url=self.ACCESS_TOKEN_URL) + request.set_parameter('oauth_verifier', verifier) request.sign_request(self._sigmethod, self._consumer, self.request_token) # send request diff --git a/tweepy/binder.py b/tweepy/binder.py index 57f11ab..fe66c7b 100644 --- a/tweepy/binder.py +++ b/tweepy/binder.py @@ -22,12 +22,28 @@ def bind_api(path, parser, allowed_param=None, method='GET', require_auth=False, else: parameters = None + # Assemble headers + headers = { + 'User-Agent': 'tweepy' + } + # Build url with parameters if parameters: url = '%s?%s' % (path, urllib.urlencode(parameters)) else: url = path + # get scheme and host + if api.secure: + scheme = 'https://' + else: + scheme = 'http://' + _host = host or api.host + + # Apply authentication + if api.auth_handler: + api.auth_handler.apply_auth(scheme + _host + url, method, headers, parameters) + # Check cache if caching enabled and method is GET if api.cache and method == 'GET': cache_result = api.cache.get(url, timeout) @@ -37,24 +53,11 @@ def bind_api(path, parser, allowed_param=None, method='GET', require_auth=False, return cache_result # Open connection - if host: - _host = host - else: - _host = api.host if api.secure: conn = httplib.HTTPSConnection(_host) else: conn = httplib.HTTPConnection(_host) - # Assemble headers - headers = { - 'User-Agent': 'tweepy' - } - - # Apply authentication - if api.auth_handler: - api.auth_handler.apply_auth(headers) - # Build request conn.request(method, url, headers=headers)