From 37d213396c25428916257e914935c9acf226d1e3 Mon Sep 17 00:00:00 2001 From: Josh Roesslein Date: Thu, 30 Jul 2009 18:06:55 -0500 Subject: [PATCH] Added support for oauth callbacks. --- tweepy/auth.py | 14 ++++++++++---- 1 file changed, 10 insertions(+), 4 deletions(-) diff --git a/tweepy/auth.py b/tweepy/auth.py index 97be9f5..63ce351 100644 --- a/tweepy/auth.py +++ b/tweepy/auth.py @@ -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 -- 2.25.1