Twitter Python API Tutorial

Playing around with the Twitter Python API.
Download:

wget http://code.google.com/p/python-twitter/downloads/detail?name=python-twitter-0.8.2.tar.gz

Extract:

tar -xvf python-twitter-0.8.2.tar.gz

Install Twitter (navigate inside the Twitter extracted folder)

sudo python setup.py install

Make sure Python Setuptool is installed

apt-get install python-setuptools

Install dependent files

httplib2-0.7.2$ sudo python setup.py install
simplejson-2.3.2$ sudo python setup.py install
simplegeo-python-oauth2-a83f4a2$ python setup.py build
simplegeo-python-oauth2-a83f4a2$ sudo python setup.py install

Register your Twitter application: https://dev.twitter.com/
Make sure you application Access Level is: Read, write, and direct messages.

Now we need to get access code for our application. Run get_access_token.py file with your consumer_key and consumer_secret inside that you got from creating your application.
Run:

sudo python get_access_token.py

It will show a link to to access with Passcode?. Click on the link to open and enter the Passcode value displayed in the webpage. It will then provide you with a access_token_key and access_token_secret which you can use in the code below:

List Twitter friends sample code:

import twitter
mytwitteraccount = twitter.Api(consumer_key='###', consumer_secret='###', access_token_key='###', access_token_secret='###')
friends=mytwitteraccount.GetFriends()
print[u.name for u in friends]

Make twitter post:

import twitter
mytwitteraccount = twitter.Api(consumer_key='###', consumer_secret='###', access_token_key='###', access_token_secret='###')
status=mytwitteraccount.PostUpdates('My first API call using Python')

4 thoughts on “Twitter Python API Tutorial

  1. Thanks you

    Thys

    This blog is help me for my first task..

    I was doing first time this such kind of for integrating twitter with python by this blog i done it very easily….

  2. This was very helpful Thys… Thank you… I was having a heck of a time importing the necessary modules…. I am trying to search Twitter for my thesis project… Could you possible show how to export tweets to MySQLdb? If not, thank you anyway for this…

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s