class AuthHandler(object):
- def apply_auth(self, headers):
+ def apply_auth(self, url, method, headers, parameters):
"""Apply authentication headers to request"""
raise NotImplemented
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"""
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)
# 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
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)
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)