Added support for oauth callbacks.
authorJosh Roesslein <jroesslein@gmail.com>
Thu, 30 Jul 2009 23:06:55 +0000 (18:06 -0500)
committerJosh Roesslein <jroesslein@gmail.com>
Thu, 30 Jul 2009 23:06:55 +0000 (18:06 -0500)
tweepy/auth.py

index 97be9f5e2bb7b6d8548ea1e35c878e26e5335400..63ce3514b6042994a167fcd6ae8f23fb388524b5 100644 (file)
@@ -3,6 +3,7 @@
 # See LICENSE
 
 from urllib2 import Request, urlopen
+from urllib import quote
 import base64
 
 import oauth
@@ -29,11 +30,12 @@ class OAuthHandler(AuthHandler):
   AUTHORIZATION_URL = 'http://twitter.com/oauth/authorize'
   ACCESS_TOKEN_URL = 'http://twitter.com/oauth/access_token'
 
-  def __init__(self, consumer_key, consumer_secrete):
+  def __init__(self, consumer_key, consumer_secrete, callback=None):
     self._consumer = oauth.OAuthConsumer(consumer_key, consumer_secrete)
     self._sigmethod = oauth.OAuthSignatureMethod_HMAC_SHA1()
     self.request_token = None
     self.access_token = None
+    self.callback = callback
 
   def apply_auth(self, url, method, headers, parameters):
     request = oauth.OAuthRequest.from_consumer_and_token(self._consumer,
@@ -44,6 +46,8 @@ class OAuthHandler(AuthHandler):
   def _get_request_token(self):
     try:
       request = oauth.OAuthRequest.from_consumer_and_token(self._consumer, http_url = self.REQUEST_TOKEN_URL)
+      if self.callback:
+        request.set_parameter('oauth_callback', self.callback)
       request.sign_request(self._sigmethod, self._consumer, None)
       resp = urlopen(Request(self.REQUEST_TOKEN_URL, headers=request.to_header()))
       return oauth.OAuthToken.from_string(resp.read())
@@ -51,25 +55,27 @@ class OAuthHandler(AuthHandler):
     except Exception, e:
       raise TweepError(e)
 
-  def get_authorization_url(self, callback=None):
+  def get_authorization_url(self):
+    """Get the authorization URL to redirect the user"""
     try:
       # get the request token
       self.request_token = self._get_request_token()
 
       # build auth request and return as url
       request = oauth.OAuthRequest.from_token_and_callback(
-          token=self.request_token, callback=callback, http_url=self.AUTHORIZATION_URL)
+          token=self.request_token, http_url=self.AUTHORIZATION_URL)
       return request.to_url()
 
     except Exception, e:
       raise TweepError(e)
 
   def get_access_token(self, verifier):
+    """After user has authorized the request token, get access token with user supplied 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.set_parameter('oauth_verifier', str(verifier))
       request.sign_request(self._sigmethod, self._consumer, self.request_token)
 
       # send request