Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
873 additions
and
1 deletion.
There are no files selected for viewing
Submodule oauthclient
deleted from
0e4669
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
Python Oauth client for Twitter | ||
--------- | ||
|
||
I built this so that i didn't have to keep looking for an oauth client for twitter to use in python. | ||
|
||
It is based off of the PHP work from abrah.am (http://github.com/poseurtech/twitteroauth/tree/master). | ||
It was very helpful. | ||
|
||
I am using the OAuth lib that is from google gdata. I figure it is a working client and is in production use - so it should be solid. You can find it at: | ||
http://gdata-python-client.googlecode.com/svn/trunk/src/gdata/oauth | ||
|
||
With a bit of modification this client should work with other publishers. | ||
|
||
btw, i am a python n00b. so feel free to help out. | ||
|
||
Thanks, | ||
harper - harper@nata2.org (email and xmpp) | ||
|
||
|
||
----------- | ||
Links: | ||
|
||
Google Code Project: http://code.google.com/p/twitteroauth-python/ | ||
Issue Tracker: http://code.google.com/p/twitteroauth-python/issues/list | ||
Wiki: http://wiki.github.com/harperreed/twitteroauth-python | ||
|
||
----------- | ||
|
||
The example client is included in the client.py. It is: | ||
|
||
if __name__ == '__main__': | ||
consumer_key = '' | ||
consumer_secret = '' | ||
while not consumer_key: | ||
consumer_key = raw_input('Please enter consumer key: ') | ||
while not consumer_secret: | ||
consumer_secret = raw_input('Please enter consumer secret: ') | ||
auth_client = TwitterOAuthClient(consumer_key,consumer_secret) | ||
tok = auth_client.get_request_token() | ||
token = tok['oauth_token'] | ||
token_secret = tok['oauth_token_secret'] | ||
url = auth_client.get_authorize_url(token) | ||
webbrowser.open(url) | ||
print "Visit this URL to authorize your app: " + url | ||
response_token = raw_input('What is the oauth_token from twitter: ') | ||
response_client = TwitterOAuthClient(consumer_key, consumer_secret,token, token_secret) | ||
tok = response_client.get_access_token() | ||
print "Making signed request" | ||
#verify user access | ||
content = response_client.oauth_request('https://twitter.com/account/verify_credentials.json', method='POST') | ||
#make an update | ||
#content = response_client.oauth_request('https://twitter.com/statuses/update.xml', {'status':'Updated from a python oauth client. awesome.'}, method='POST') | ||
print content | ||
|
||
print 'Done.' | ||
|
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,156 @@ | ||
''' | ||
Python Oauth client for Twitter | ||
Used the SampleClient from the OAUTH.org example python client as basis. | ||
props to leahculver for making a very hard to use but in the end usable oauth lib. | ||
''' | ||
import httplib | ||
import urllib | ||
import time | ||
import webbrowser | ||
import oauth as oauth | ||
from urlparse import urlparse | ||
|
||
|
||
class TwitterOAuthClient(oauth.OAuthClient): | ||
api_root_url = 'https://twitter.com' #for testing 'http://term.ie' | ||
api_root_port = "80" | ||
|
||
#set api urls | ||
def request_token_url(self): | ||
return self.api_root_url + '/oauth/request_token' | ||
def authorize_url(self): | ||
return self.api_root_url + '/oauth/authorize' | ||
def authenticate_url(self): | ||
return self.api_root_url + '/oauth/authenticate' | ||
def access_token_url(self): | ||
return self.api_root_url + '/oauth/access_token' | ||
|
||
#oauth object | ||
def __init__(self, consumer_key, consumer_secret, oauth_token=None, oauth_token_secret=None): | ||
self.sha1_method = oauth.OAuthSignatureMethod_HMAC_SHA1() | ||
self.consumer = oauth.OAuthConsumer(consumer_key, consumer_secret) | ||
if ((oauth_token != None) and (oauth_token_secret!=None)): | ||
self.token = oauth.OAuthConsumer(oauth_token, oauth_token_secret) | ||
else: | ||
self.token = None | ||
|
||
def oauth_request(self,url, args = {}, method=None): | ||
if (method==None): | ||
if args=={}: | ||
|
||
method = "GET" | ||
else: | ||
method = "POST" | ||
req = oauth.OAuthRequest.from_consumer_and_token(self.consumer, self.token, method, url, args) | ||
req.sign_request(self.sha1_method, self.consumer,self.token) | ||
if (method=="GET"): | ||
return self.http_wrapper(req.to_url()) | ||
elif (method == "POST"): | ||
return self.http_wrapper(req.get_normalized_http_url(),req.to_postdata()) | ||
|
||
# trying to make a more robust http wrapper. this is a failure ;) | ||
def http_wrapper_fucked(self, url, postdata=""): | ||
parsed_url = urlparse(url) | ||
connection_url = parsed_url.path+"?"+parsed_url.query | ||
hostname = parsed_url.hostname | ||
scheme = parsed_url.scheme | ||
headers = {'Content-Type' :'application/x-www-form-urlencoded'} | ||
if scheme=="https": | ||
connection = httplib.HTTPSConnection(hostname) | ||
else: | ||
connection = httplib.HTTPConnection(hostname) | ||
connection.request("POST", connection_url, body=postdata, headers=headers) | ||
connection_response = connection.getresponse() | ||
self.last_http_status = connection_response.status | ||
self.last_api_call= url | ||
response= connection_response.read() | ||
|
||
#this is barely working. (i think. mostly it is that everyone else is using httplib) | ||
def http_wrapper(self, url, postdata={}): | ||
try: | ||
if (postdata != {}): | ||
f = urllib.urlopen(url, postdata) | ||
else: | ||
f = urllib.urlopen(url) | ||
response = f.read() | ||
except: | ||
response = "" | ||
return response | ||
|
||
|
||
def get_request_token(self): | ||
response = self.oauth_request(self.request_token_url()) | ||
token = self.oauth_parse_response(response) | ||
try: | ||
self.token = oauth.OAuthConsumer(token['oauth_token'],token['oauth_token_secret']) | ||
return token | ||
except: | ||
raise oauth.OAuthError('Invalid oauth_token') | ||
|
||
def oauth_parse_response(self, response_string): | ||
r = {} | ||
for param in response_string.split("&"): | ||
pair = param.split("=") | ||
if (len(pair)!=2): | ||
break | ||
|
||
r[pair[0]]=pair[1] | ||
return r | ||
|
||
def get_authorize_url(self, token): | ||
return self.authorize_url() + '?oauth_token=' +token | ||
|
||
def get_authenticate_url(self, token): | ||
return self.authenticate_url() + '?oauth_token=' +token | ||
|
||
def get_access_token(self,token=None): | ||
r = self.oauth_request(self.access_token_url()) | ||
token = self.oauth_parse_response(r) | ||
self.token = oauth.OAuthConsumer(token['oauth_token'],token['oauth_token_secret']) | ||
return token | ||
|
||
def oauth_request(self, url, args={}, method=None): | ||
if (method==None): | ||
if args=={}: | ||
method = "GET" | ||
else: | ||
method = "POST" | ||
req = oauth.OAuthRequest.from_consumer_and_token(self.consumer, self.token, method, url, args) | ||
req.sign_request(self.sha1_method, self.consumer,self.token) | ||
if (method=="GET"): | ||
return self.http_wrapper(req.to_url()) | ||
elif (method == "POST"): | ||
return self.http_wrapper(req.get_normalized_http_url(),req.to_postdata()) | ||
|
||
|
||
|
||
if __name__ == '__main__': | ||
consumer_key = '' | ||
consumer_secret = '' | ||
while not consumer_key: | ||
consumer_key = raw_input('Please enter consumer key: ') | ||
while not consumer_secret: | ||
consumer_secret = raw_input('Please enter consumer secret: ') | ||
auth_client = TwitterOAuthClient(consumer_key,consumer_secret) | ||
tok = auth_client.get_request_token() | ||
token = tok['oauth_token'] | ||
token_secret = tok['oauth_token_secret'] | ||
url = auth_client.get_authorize_url(token) | ||
webbrowser.open(url) | ||
print "Visit this URL to authorize your app: " + url | ||
response_token = raw_input('What is the oauth_token from twitter: ') | ||
response_client = TwitterOAuthClient(consumer_key, consumer_secret,token, token_secret) | ||
tok = response_client.get_access_token() | ||
print "Making signed request" | ||
#verify user access | ||
content = response_client.oauth_request('https://twitter.com/account/verify_credentials.json', method='POST') | ||
#make an update | ||
#content = response_client.oauth_request('https://twitter.com/statuses/update.xml', {'status':'Updated from a python oauth client. awesome.'}, method='POST') | ||
print content | ||
|
||
print 'Done.' | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
1. Moved oauth.py to __init__.py | ||
|
||
2. Refactored __init__.py for compatibility with python 2.2 (Issue 59) | ||
|
||
3. Refactored rsa.py for compatibility with python 2.2 (Issue 59) | ||
|
||
4. Refactored OAuthRequest.from_token_and_callback since the callback url was | ||
getting double url-encoding the callback url in place of single. (Issue 43) | ||
|
||
5. Added build_signature_base_string method to rsa.py since it used the | ||
implementation of this method from oauth.OAuthSignatureMethod_HMAC_SHA1 which | ||
was incorrect since it enforced the presence of a consumer secret and a token | ||
secret. Also, changed its super class from oauth.OAuthSignatureMethod_HMAC_SHA1 | ||
to oauth.OAuthSignatureMethod (Issue 64) | ||
|
||
6. Refactored <OAuthRequest>.to_header method since it returned non-oauth params | ||
as well which was incorrect. (Issue 31) |
Oops, something went wrong.