Twitter From the Command Line in Python Using OAuth

Issuing Twitter updates from the command line was once a simple affair: you could just use curl or its equivalent. Things changed when basic authentication was removed from the Twitter API in August 2010, and now all Twitter clients are required to use secure authentication based on OAuth.

Configuring an app to use OAuth properly requires a bit of legwork. My goal with this post is to save people time by showing the complete step-by-step process of building a Python script that can tweet from the command line using OAuth. I’ve intentionally skipped over the details of how OAuth works, and what the different authentication tokens mean. This post is just about getting things done.

Step 1: Download Tweepy

Tweepy is an awesome Twitter library for Python. Much of this post is based on information I found in the documentation for Tweepy.

Download Tweepy from GitHub and install it on your system.

Step 2: Register a new client app with Twitter

Navigate to http://twitter.com/oauth_clients and click on Register a new application. You might have to log in to the Twitter site first, if you’re not already.

Fill in the registration fields as follows:

Note: whatever you specify for Application Name will be the “via” name your followers see in the details of tweets issued from your command line app:

When finished on the registration page, click Save.

Next page:

Keep this browser window open. We’ll need the information in the next step.

Step 3: Connect the app to your Twitter account

Next, the app needs to be authorized to connect to your account so it can send tweets under your name.

We’ll create a one-off utility script to do this. Save the following Python code as a script on your local system.

#!/usr/bin/env python

import tweepy

CONSUMER_KEY = 'paste your Consumer Key here'
CONSUMER_SECRET = 'paste your Consumer Secret here'

auth = tweepy.OAuthHandler(CONSUMER_KEY, CONSUMER_SECRET)
auth.secure = True
auth_url = auth.get_authorization_url()
print 'Please authorize: ' + auth_url
verifier = raw_input('PIN: ').strip()
auth.get_access_token(verifier)
print "ACCESS_KEY = '%s'" % auth.access_token.key
print "ACCESS_SECRET = '%s'" % auth.access_token.secret

Paste the Consumer Key and Consumer Secret from the end of step 2 into this script, replacing the CONSUMER_KEY and CONSUMER_SECRET constants. Then save and run on your system.

You should see a prompt like this:

Please authorize: <URL>
PIN:

Open that URL in your browser. You should see the standard OAuth Twitter connection screen:

Click Allow.

Twitter will then provide you with a PIN code that authenticates the connection between the client app and your Twitter account.

Enter this PIN into the prompt from the Python registration script:

PIN: 2781961

The script will then print out another key/secret pair:

ACCESS_KEY = '124242RCyi3g0cZ4r5BWL047rsh0S0yv5VxAGwTKCOsHAb'
ACCESS_SECRET = 'kaTXiC489qo8y6haTBSlwOqR1syG83tzPG2StdQ'

But the values will be different each time.

Keep this information on your screen because we’ll need it in the next step.

Step 4: Create the command line script

Save the following Python code as a script on your local system.

#!/usr/bin/env python

import sys
import tweepy

CONSUMER_KEY = 'paste your Consumer Key here'
CONSUMER_SECRET = 'paste your Consumer Secret here'
ACCESS_KEY = 'paste your Access Key here'
ACCESS_SECRET = 'paste your Access Secret here'

auth = tweepy.OAuthHandler(CONSUMER_KEY, CONSUMER_SECRET)
auth.secure = True
auth.set_access_token(ACCESS_KEY, ACCESS_SECRET)
api = tweepy.API(auth)
api.update_status(sys.argv[1])

Paste the Access Key and Access Secret from the end of step 3 into this script, replacing the ACCESS_KEY and ACCESS_SECRET constants.

You’ll also need to paste in the CONSUMER_KEY and CONSUMER_VALUES from step 2, just as we did in the registration script.

Save the final script as mycommandlineapp or whatever you want to name it.

Step 5: Send a test tweet from the command line

Finally, we’re all set up. Our command line app is registered as a Twitter client and the app is connected to our Twitter user account.

Try sending a test tweet from the command line. Don’t forget to enclose the text in quotes.

$ ./mycommandlineapp 'Hello from the command line'

If this is successful, there won’t be any output on the command line, but there will be a new tweet in your timeline:

Done.