Sunday, November 15, 2009

Twitter oauth with python for desktop applications

As an opportunity to learn about oauth and create a small twitter client (under progress), I googled and found this django snippet, which is very close to what I needed. Although it is perfect if you want to incorporate twitter into a django app using oauth but still requires a little bit of addition if you want to use it in a desktop application.

According to twitter's wiki page on authentication, in the section for desktop clients, it states that after you authorize the application to use your account you'll receive a seven digit pin code. This pin code must be sent as an "oauth_verifier" parameter in the next request to request an access token. In the method "exchange_request_token_for_access_token" I've added an argument "pin" which will take in the pin provided on the authorization step. Apart from that I've also changed the oauth authentication urls a bit, and used httplib's HTTPConnection instead of the HTTPSConnection. I don't know why but the secure connection wasn't working for me. Anyway, you if anyone gets it working with HTTPSConnection, do tell me.

The code requires the following external libraries:

NOTE: THIS CODE IS NOT MEANT TO BE USED IN A PRODUCTION ENVIRONMENT. I SHALL NOT BE RESPONSIBLE FOR ANY DAMAGE CAUSED.


import oauth.oauth as oauth
import simplejson
import httplib


consumer_key="key"
consumer_secret="secret"
request_token_url='http://twitter.com/oauth/request_token'
access_token_url='http://twitter.com/oauth/access_token'
authorize_url='http://twitter.com/oauth/authorize'
twitter_check_auth='http://twitter.com/account/verify_credentials.json'
twitter_statuses_update='http://twitter.com/statuses/update.json'
server="twitter.com"

connection = httplib.HTTPConnection(server)
consumer = oauth.OAuthConsumer(consumer_key, consumer_secret)
signature_method = oauth.OAuthSignatureMethod_HMAC_SHA1()


def fetch_response(oauth_request, connection):
url = oauth_request.to_url()
connection.request(oauth_request.http_method, url)
response = connection.getresponse().read()
return response

def post(oauth_request, connection):
url = oauth_request.to_url()
connection.request('POST', url)
response = connection.getresponse().read()
return response

def get_unauthorised_request_token():
oauth_request = oauth.OAuthRequest.from_consumer_and_token(consumer, http_url=request_token_url)
oauth_request.sign_request(signature_method, consumer, None)
resp = fetch_response(oauth_request, connection)
token = oauth.OAuthToken.from_string(resp)
return token

def get_authorisation_url(token):
oauth_request = oauth.OAuthRequest.from_consumer_and_token(consumer, token=token, http_url=authorize_url)
oauth_request.sign_request(signature_method, consumer, token)
return oauth_request.to_url()

def exchange_request_token_for_access_token(request_token, pin):
oauth_request = oauth.OAuthRequest.from_consumer_and_token(consumer, token=request_token, http_url=access_token_url, parameters={'oauth_verifier':pin})
oauth_request.sign_request(signature_method, consumer, request_token)
resp = fetch_response(oauth_request, connection)
return oauth.OAuthToken.from_string(resp)

def request(url, access_token, parameters=None, http_method="GET"):
oauth_request = oauth.OAuthRequest.from_consumer_and_token(consumer,token=access_token, http_url=url, parameters=parameters, http_method=http_method)
oauth_request.sign_request(signature_method, consumer, access_token)
return oauth_request

def is_authenticated(access_token):
oauth_request = request(twitter_check_auth, access_token)
json=fetch_response(oauth_request, connection)
if 'screen_name' in json:
return json
return False

def update_status(access_token, status):
oauth_request = request(twitter_statuses_update, access_token, parameters = {'status':status}, http_method="POST")
json = fetch_response(oauth_request, connection)
if 'screen_name' in json:
return json
return False

No comments: