Finished up oauth handler. Tested and working.
authorJosh Roesslein <jroesslein@gmail.com>
Thu, 30 Jul 2009 22:22:04 +0000 (17:22 -0500)
committerJosh Roesslein <jroesslein@gmail.com>
Thu, 30 Jul 2009 22:22:04 +0000 (17:22 -0500)
tweepy/auth.py
tweepy/binder.py

index db7db97d5179a7dbe310f1d859dfb47402f61a73..97be9f5e2bb7b6d8548ea1e35c878e26e5335400 100644 (file)
@@ -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
index 57f11ab3f85306a79cc43e72db42769cb36a6bc2..fe66c7b6124d7680cc995d8e1068f423c5e8dba8 100644 (file)
@@ -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)