Accessing Twitter with Python

As a building block for future projects I wanted to learn how to post to twitter using Python. There’s quite a bit of help and information out there, some of it out of date as it appears the API has changed particularly in the area of authentication. Not sure when this happened, but some tutorials teach the obsolete username/password method whilst others use the current OAuth method. The python.org’s page on the module was, of course, the most up to date.

This post assumes you’re using a Raspberry Pi for this.  Anyway, the building blocks I used to get this going are as follows:

  1. If you haven’t already, create a twitter account at twitter.com.
  2. Log into Twitter Developers with your twitter account.
  3. Create an application, doesn’t really matter what it’d called, we just want the credentials for use in out Python script.
  4. Set the permissions as required from read, write and access direct messages.
  5. Save the following credentials somewhere safe:
    1. OAuth: Consumer Key
    2. OAuth: Consumer Secret
    3. Access Token
    4. Access Token Secret

Download and install the python-twitter module (the python-oauth2 module is also required):

# sudo apt-get install python-twitter
# sudo apt-get install python-oath2

I didn’t want to store these credentials within the script in case I wanted to share the code! So created a configuration file (twitter.cfg) with the following contents:

# Contents of configuration file: twitter.cfg
#
[DEFAULT]
consumer_key = *********************
consumer_secret = **********************
access_token_key = **********************
access_token_secret = ****************

These credentials are then read from the file using the Python ConfigParser library.

The example code below simply reads the config file, uses the credentials to connect to twitter and update the status:

# Contents of configuration file: twitter.cfg
#
# [DEFAULT]
# consumer_key = *********************
# consumer_secret = *******************************************
# access_token_key = **************************************************
# access_token_secret = ******************************************

import twitter
import ConfigParser

config = ConfigParser.RawConfigParser()

try:
    config.readfp(open('twitter.cfg'))
except IOError as e:
    print "I/O error ({0}): ({1})".format(e.errno, e.strerror)
    exit(1)
try:
    myConsumerKey = config.get('DEFAULT', 'consumer_key')
    myConsumerSecret = config.get('DEFAULT', 'consumer_secret')
    myAccessTokenKey = config.get('DEFAULT', 'access_token_key')
    myAccessTokenSecret = config.get('DEFAULT', 'access_token_secret')
except ConfigParser.NoSectionError as e:
    print "ConfigParser ({0})".format(e)
    exit(1)
except ConfigParser.NoOptionError as e:
    print "ConfigParser ({0})".format(e)
    exit(1)

client = twitter.Api(consumer_key=myConsumerKey,
                  consumer_secret=myConsumerSecret,
                  access_token_key=myAccessTokenKey,
                  access_token_secret=myAccessTokenSecret)

try:
    update = client.PostUpdate('The Twitter API is easy')
except twitter.TwitterError as e:
    print "TwitterError ({0})".format(e)
    exit(1)

print update.text

Leave a Reply

Your email address will not be published. Required fields are marked *