Sunday, December 16, 2012

Update Twitter Using Python Tweepy


I just spending sometime to play with tweepy. This post will show how to use that lib to access Twitter API.

First, install python (obviously). Second, install (if not installed yet) easy_install a tool to install python package easily. Refer to this http://pypi.python.org/pypi/setuptools

Go to console and check if easy_install instalation.



Now install tweepy module if not available yet.


Next create application in twitter so we can access the API. Go to this URL: https://dev.twitter.com/apps and create new application


Please note in there is a configuration to set application type.


After application has been created, we need some special authorization to access API. Twitter use OAuth. There are 4 important values that we have to note:
OAuth Consumer Key
OAuth Consumer Secret


and
Access Token
Access Token Secret


The last two are the token which is granted by our application user later.

Enough for configuration, now go to python console (I am using iPython) evaluate following code:

import tweepy
# provide all the keys and secrets
CONSUMER_KEY = 'the_key'
CONSUMER_SECRET = 'the_secret'
ACCESS_TOKEN = 'the_token'
ACCESS_SECRET = 'the_token_secret'
auth = tweepy.OAuthHandler(CONSUMER_KEY, CONSUMER_SECRET)
auth.set_access_token(ACCESS_TOKEN,ACCESS_SECRET)
api = tweepy.API(auth)

# do anything you want now
api.friends()
api.update_status('Hello world from Tweepy')

No comments:

Post a Comment