Added callback_url parameter to Twython to set the URL the remote

site will use to return the request token.

Supports both OAuth 1.0 and 1.0a methods of setting the oauth_callback
parameter. For OAuth 1.0a support, a patched version of python-oauth2
is currently required (see
https://github.com/simplegeo/python-oauth2/pull/43).

Availability of OAuth 1.0a support in python-oauth2 is autodetected
and a suitable warning is given if the remote site requires it but
python-oauth2 doesn't support it.
This commit is contained in:
Erik Scheffers 2011-01-18 22:14:11 +08:00 committed by Ryan McGrath
parent e5bef0f8e4
commit 0737c12449

View file

@ -19,6 +19,7 @@ import httplib2
import mimetypes
import mimetools
import re
import inspect
import oauth2 as oauth
@ -46,6 +47,9 @@ except ImportError:
# Seriously wtf is wrong with you if you get this Exception.
raise Exception("Twython requires the simplejson library (or Python 2.6) to work. http://www.undefined.org/python/")
# Detect if oauth2 supports the callback_url argument to request
OAUTH_LIB_SUPPORTS_CALLBACK = 'callback_url' in inspect.getargspec(oauth.Client.request).args
class TwythonError(AttributeError):
"""
Generic error class, catch-all for most Twython issues.
@ -91,7 +95,7 @@ class AuthError(TwythonError):
class Twython(object):
def __init__(self, twitter_token = None, twitter_secret = None, oauth_token = None, oauth_token_secret = None, headers=None):
def __init__(self, twitter_token = None, twitter_secret = None, oauth_token = None, oauth_token_secret = None, headers=None, callback_url=None):
"""setup(self, oauth_token = None, headers = None)
Instantiates an instance of Twython. Takes optional parameters for authentication and such (see below).
@ -115,6 +119,7 @@ class Twython(object):
self.twitter_secret = twitter_secret
self.oauth_token = oauth_token
self.oauth_secret = oauth_token_secret
self.callback_url = callback_url
# If there's headers, set them, otherwise be an embarassing parent for their own good.
self.headers = headers
@ -184,13 +189,37 @@ class Twython(object):
Returns an authorization URL for a user to hit.
"""
resp, content = self.client.request(self.request_token_url, "GET")
callback_url = self.callback_url or 'oob'
request_args = {}
if OAUTH_LIB_SUPPORTS_CALLBACK:
request_args['callback_url'] = callback_url
resp, content = self.client.request(self.request_token_url, "GET", **request_args)
if resp['status'] != '200':
raise AuthError("Seems something couldn't be verified with your OAuth junk. Error: %s, Message: %s" % (resp['status'], content))
request_tokens = dict(urlparse.parse_qsl(content))
request_tokens['auth_url'] = "%s?oauth_token=%s" % (self.authenticate_url, request_tokens['oauth_token'])
oauth_callback_confirmed = request_tokens.get('oauth_callback_confirmed')=='true'
if not OAUTH_LIB_SUPPORTS_CALLBACK and callback_url!='oob' and oauth_callback_confirmed:
import warnings
warnings.warn("oauth2 library doesn't support OAuth 1.0a type callback, but remote requires it")
oauth_callback_confirmed = False
auth_url_params = {
'oauth_token' : request_tokens['oauth_token'],
}
# Use old-style callback argument
if callback_url!='oob' and not oauth_callback_confirmed:
auth_url_params['oauth_callback'] = callback_url
request_tokens['auth_url'] = self.authenticate_url + '?' + urllib.urlencode(auth_url_params)
print request_tokens
return request_tokens
def get_authorized_tokens(self):