# See LICENSE
from urllib2 import Request, urlopen
+from urllib import quote
import base64
import oauth
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,
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())
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