Sunday, May 6th, 2012

Getting started with Python and Twitter

There are many Twitter Python APIs to choose from on PyPI. Some have additional features than others, some are required by other packages for Twitter authentication purposes. Since I plan on building my own Twitter authentication app for Django, which tailors to my specific needs, I went ahead and located an easy to use Twitter API. I ended up finding twitter. Although using this name is against Twitter's agreement, but that's another story.

Installing this API is very simple, it can be install either by using easy_install or pip, the latter is more preferred:

$ pip install twitter

It also comes with a command-line tool to enable you to update your status via your command-line. In order to use this API, you will need to register an application with Twitter on dev.twitter.com. Once you have your consumer and personal access tokens from the application settings page, change the permissions of the application to Read/Write so that you can also update your status. In a Python shell, here is how you would authenticate and update your status:

>>> import twitter
>>> auth = twitter.OAuth(token, token_secret, consumer_key, consumer_secret)
>>> t = twitter.Twitter(auth=auth)
>>> t.account.verify_credentials()
>>> t.statuses.update(status="Testing the Twitter API via #Python for future applications.")

The second last command will just make sure that everything is working correctly, and will also output your current account information, including your last tweet. If successfully, you will see your status update appear on Twitter almost immediately.

I will highly recommend viewing the API documentation on the Twitter developer page. It explains how to use the API to grab lots of useful information, and how to update information.

As I have had many requests to add a Twitter notification system to my blog, I plan on implementing this feature for new posts. I also plan on adding a Tweet button, similar to the Google +1 button, for easy sharing of posts on Twitter. The Twitter button will use Twitter's standard API for such buttons, not the Python API.

About Me

My Photo
Names Kevin, hugely into UNIX technologies, not just Linux. I've dabbled with the demons, played with the Sun, and now with the Penguins.




Kevin Veroneau Consulting Services
Do you require the services of a Django contractor? Do you need both a website and hosting services? Perhaps I can help.

This Month

If you like what you read, please consider donating to help with hosting costs, and to fund future books to review.

Python Powered | © 2012-2013 Kevin Veroneau