Some very experimental, probably not working yet, OAuth related pieces. Heavily inspired by
henriklied's 'django-twitter-oauth' work.
This commit is contained in:
parent
0331eb0612
commit
f7df3c3bae
1 changed files with 41 additions and 19 deletions
60
twython.py
60
twython.py
|
|
@ -54,7 +54,7 @@ class AuthError(TwythonError):
|
||||||
return repr(self.msg)
|
return repr(self.msg)
|
||||||
|
|
||||||
class setup:
|
class setup:
|
||||||
def __init__(self, authtype = "OAuth", username = None, password = None, consumer_secret = None, consumer_key = None, headers = None):
|
def __init__(self, username = None, password = None, consumer_key = None, consumer_secret = None, signature_method = None, headers = None):
|
||||||
"""setup(authtype = "OAuth", username = None, password = None, consumer_secret = None, consumer_key = None, headers = None)
|
"""setup(authtype = "OAuth", username = None, password = None, consumer_secret = None, consumer_key = None, headers = None)
|
||||||
|
|
||||||
Instantiates an instance of Twython. Takes optional parameters for authentication and such (see below).
|
Instantiates an instance of Twython. Takes optional parameters for authentication and such (see below).
|
||||||
|
|
@ -64,6 +64,7 @@ class setup:
|
||||||
password - Password for your twitter account, if you want Basic (HTTP) Authentication.
|
password - Password for your twitter account, if you want Basic (HTTP) Authentication.
|
||||||
consumer_secret - Consumer secret, if you want OAuth.
|
consumer_secret - Consumer secret, if you want OAuth.
|
||||||
consumer_key - Consumer key, if you want OAuth.
|
consumer_key - Consumer key, if you want OAuth.
|
||||||
|
signature_method - Method for signing OAuth requests; defaults to oauth.OAuthSignatureMethod_HMAC_SHA1()
|
||||||
headers - User agent header.
|
headers - User agent header.
|
||||||
"""
|
"""
|
||||||
self.authenticated = False
|
self.authenticated = False
|
||||||
|
|
@ -77,6 +78,9 @@ class setup:
|
||||||
self.consumer_secret = consumer_secret
|
self.consumer_secret = consumer_secret
|
||||||
self.request_token = None
|
self.request_token = None
|
||||||
self.access_token = None
|
self.access_token = None
|
||||||
|
self.consumer = None
|
||||||
|
self.connection = None
|
||||||
|
self.signature_method = None
|
||||||
# Check and set up authentication
|
# Check and set up authentication
|
||||||
if self.username is not None and password is not None:
|
if self.username is not None and password is not None:
|
||||||
# Assume Basic authentication ritual
|
# Assume Basic authentication ritual
|
||||||
|
|
@ -92,30 +96,48 @@ class setup:
|
||||||
except HTTPError, e:
|
except HTTPError, e:
|
||||||
raise TwythonError("Authentication failed with your provided credentials. Try again? (%s failure)" % `e.code`, e.code)
|
raise TwythonError("Authentication failed with your provided credentials. Try again? (%s failure)" % `e.code`, e.code)
|
||||||
elif consumer_secret is not None and consumer_key is not None:
|
elif consumer_secret is not None and consumer_key is not None:
|
||||||
|
self.consumer = oauth.OAuthConsumer(self.consumer_key, self.consumer_secret)
|
||||||
|
self.connection = httplib.HTTPSConnection(SERVER)
|
||||||
self.signature_method = oauth.OAuthSignatureMethod_HMAC_SHA1()
|
self.signature_method = oauth.OAuthSignatureMethod_HMAC_SHA1()
|
||||||
# Awesome OAuth authentication ritual
|
|
||||||
# req = oauth.OAuthRequest.from_consumer_and_token
|
|
||||||
# req.sign_request(self.signature_method, self.consumer_key, self.token)
|
|
||||||
# self.opener = urllib2.build_opener()
|
|
||||||
pass
|
pass
|
||||||
else:
|
else:
|
||||||
pass
|
pass
|
||||||
|
|
||||||
def getRequestToken(self):
|
def getOAuthResource(self, url, access_token, params, http_method="GET"):
|
||||||
response = self.oauth_request(self.request_token_url)
|
"""getOAuthResource(self, url, access_token, params, http_method="GET")
|
||||||
token = self.parseOAuthResponse(response)
|
|
||||||
self.request_token = oauth.OAuthConsumer(token['oauth_token'],token['oauth_token_secret'])
|
|
||||||
return self.request_token
|
|
||||||
|
|
||||||
def parseOAuthResponse(self, response_string):
|
Returns a signed OAuth object for use in requests.
|
||||||
# Partial credit goes to Harper Reed for this gem.
|
"""
|
||||||
lol = {}
|
newRequest = oauth.OAuthRequest.from_consumer_and_token(consumer, token=self.access_token, http_method=http_method, http_url=url, parameters=parameters)
|
||||||
for param in response_string.split("&"):
|
oauth_request.sign_request(self.signature_method, consumer, access_token)
|
||||||
pair = param.split("=")
|
return oauth_request
|
||||||
if(len(pair) != 2):
|
|
||||||
break
|
def getResponse(self, oauth_request, connection):
|
||||||
lol[pair[0]] = pair[1]
|
"""getResponse(self, oauth_request, connection)
|
||||||
return lol
|
|
||||||
|
Returns a JSON-ified list of results.
|
||||||
|
"""
|
||||||
|
url = oauth_request.to_url()
|
||||||
|
connection.request(oauth_request.http_method, url)
|
||||||
|
response = connection.getresponse()
|
||||||
|
return simplejson.load(response.read())
|
||||||
|
|
||||||
|
def getUnauthorisedRequestToken(self, consumer, connection, signature_method=self.signature_method):
|
||||||
|
oauth_request = oauth.OAuthRequest.from_consumer_and_token(consumer, consumer, http_url=self.request_token_url)
|
||||||
|
oauth_request.sign_request(signature_method, consumer, None)
|
||||||
|
resp = fetch_response(oauth_request, connection)
|
||||||
|
return oauth.OAuthToken.from_string(resp)
|
||||||
|
|
||||||
|
def getAuthorizationURL(self, consumer, token, signature_method=self.signature_method):
|
||||||
|
oauth_request = oauth.OAuthRequest.from_consumer_and_token(consumer, token=token, http_url=self.authorization_url)
|
||||||
|
oauth_request.sign_request(signature_method, consumer, token)
|
||||||
|
return oauth_request.to_url()
|
||||||
|
|
||||||
|
def exchangeRequestTokenForAccessToken(self, consumer, connection, request_token=self.request_token, signature_method=self.signature_method):
|
||||||
|
oauth_request = oauth.OAuthRequest.from_consumer_and_token(consumer, token=request_token, http_url=self.access_token_url)
|
||||||
|
oauth_request.sign_request(signature_method, consumer, request_token)
|
||||||
|
resp = fetch_response(oauth_request, connection)
|
||||||
|
return oauth.OAuthToken.from_string(resp)
|
||||||
|
|
||||||
# URL Shortening function huzzah
|
# URL Shortening function huzzah
|
||||||
def shortenURL(self, url_to_shorten, shortener = "http://is.gd/api.php", query = "longurl"):
|
def shortenURL(self, url_to_shorten, shortener = "http://is.gd/api.php", query = "longurl"):
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue