Python 3 and 2 Meet Twitter


There has been a lot of confusion with regards to accessing twitter with python. For people like me who had used python 2.4 etc with twitter are wondering why their scripts are not working anymore. I dug mine out from an old backup only to find that it not only has problems with twitter but also with python 3 – The shiny new thing.

Twitter has upgraded and now basic authentication is now disabled. Instead they use OAuth or Open Authentication. OAuth allows users to hand out tokens instead of credentials to their data hosted by a given service provider. Each token grants access to a specific site for specific resources and for a defined duration. This allows a user to grant a third party site access to their information stored with another service provider, without sharing their access permissions or the full extent of their data. There is a lot of info available at dev.twitter.com

I downloaded the shiny new python 3 for windows only to find out that a lot of packages and apps were missing. I tried a lot of thing but was not very successful. Since support for python (and other programming languages) is better in linux, I ran through some online guides and was able to get something running.
The tweepy API can be used and the following function is what can be used to send a twitter update:

def send_to_twitter(msg):
import sys
import tweepy
CONSUMER_KEY = ‘…’
CONSUMER_SECRET = ‘…’
ACCESS_KEY = ‘…’
ACCESS_SECRET = ‘…’
auth = tweepy.OAuthHandler(CONSUMER_KEY, CONSUMER_SECRET)
auth.set_access_token(ACCESS_KEY, ACCESS_SECRET)
api = tweepy.API(auth)
api.update_status(msg)

To get this running, on ubuntu the following solution worked:

  1. Download tweepy 1.3 for python3 from code.google.com
  2. Create folder tweepy somewhere in your home directory and unzip tweepy there with tar -xzvf tweepy-1.4-py3.tar.gz command.
  3. Download distribute_setup.py Distribute is the setuptools fork for python3.
  4. Run distribute installation with sudo python3 distribute_setup.py command.
  5. Go to folder, where you extracted tweepy archive and run sudo python3 setup.py install – this will install tweepy library for your computer.
  6. Use instructions for authenticating your program with twitter as explained here
  7. Define a function as above.

and Done.

With Windows however I decided it was not worth the effort and I decided to retreat to python 2.7

There is an O Riley forum out there at

http://forums.oreilly.com/content/Head-First-Programming/20756/Sending-Messages-To-Twitter/

Apparently there is a book for python that has explained basic authentication. The last post in the thread is given on how to install tweepy for python 3. I have not tried the same because right now setup tools and easy install is not available for Python 3.x so if I get stuck somewhere later, I will have a laundry list of things to do to make the script work back in py2.x so I am not migrating to python 3 until the base support and package library is not stronger.

With python 2.7 the process is simple.

Download the tweepy package for windows and run python setup.py install and you are done. It includes all the Oauth packs etc.

Instead of copy and paste the text from the original authors I have given the useful link above. Someone posted about different twitter APIs and how one was better than the other so if someone is actually interested they can go ahead and look into that. I just wanted to get the powers of tweeting via python back so mission accomplished for now.

Advertisements

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 )

Google+ photo

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

Connecting to %s