Surviving the Twitter OAuthcalypse with Tweet-A-Watt

When Twitter recently made the OAuth API mandatory for 3rd party applications, the resulting OAuthcalypse silenced the tweets of a lot of code.

Among those affected was Adafruit‘s innovative Tweet-A-Watt system, which interfaces with a low cost Kill-A-Watt monitor to track your electrical usage. After its original release the Tweet-A-Watt’s software support was expanded to include more sophisticated platforms, such as Google’s App Engine and GoogleMeter, but I found I missed the original Twitter functionality. What’s Tweet-A-Watt without the tweets?

Unfortunately, supporting OAuth isn’t as simple as updating a python script or two. OAuth is a far more sophisticated system than the basic user-ID/password authentication that Twitter used to support. (Though, as we learned this week, it seems that Twitter bolted the front door and left a back door open. Oops!)

A look through the “Beginner’s Guide to OAuth” might make you wonder how far down from a beginner you rank. A handy resource is this excellent blog post by Jeff Miller, which tells you all that you need to know to make Python software talk to OAuth.

The bad news is that the latest release of python-twitter API used by Tweet-A-Watt’s doesn’t yet support OAuth. That will change – eventually. OAuth support has been added in their development builds, and perhaps by the time you read this a new release might include it. You can check their download page here.

The other bad news is that, even with an API that supports OAuth, you’re going to have to get some authentication “tokens” of your own from Twitter – these replace your Twitter user-ID and password in the wattcher script. The process is relatively straightforward, and fully explained in Steps 2 and 3 of Jeff Miller’s post, but requires the somewhat intimidating process of registering “your application” with Twitter. Fortunately, Twitter is not at all choosy about who and what qualifies as an application, so don’t be shy about signing up.

If you followed the steps in Jeff Miller’s post, then you’ve become somewhat acquainted with the Tweepy API, an alternative to python-twitter which has supported OAuth for some time. Tweepy has excellent documentation and is quite easy to understand, so I decided that the easiest path would be to modify to use Tweepy instead of python-twitter. Here is what’s required:

1. Install Tweepy on the box which will run wattcher. I use a low-powered Linux “plug computer” (Tonido Plug) for this, since running a full desktop PC 24/7 to monitor your electrical usage is both ironic and dumb. TonidoPlug runs Ubuntu Linux, and installing Tweepy in Ubuntu is a snap:

$ 	aptitude install python-setuptools
$ 	easy_install tweepy

2. The first change to is to replace the python-twitter API with Tweepy:

#import twitter
import tweepy

3. The Twitter username and password constants are no longer needed – they are replaced with the 4 long hash strings that you’ll get from following the steps in Jeff Miller’s post:

# twitterusername = "username"
# twitterpassword = "password"
CONSUMER_KEY = 'your_consumer_key'
CONSUMER_SECRET = 'your_consumer_secret'
ACCESS_KEY = 'your_access_key'
ACCESS_SECRET = 'your_access_secret'

4. Your code needs to “login” to Twitter by calling a few Tweepy API functions. I put this code in the mainline of, right below the constants added in step 3. This means that wattcher logs in just once – if it somehow gets logged out or needs to re-authenticate, it is out of luck. It might be safer to login every time you tweet, as wattcher previously did in its TwitterIt function. However, I haven’t run into any problems with the approach I took so far (it has been running about 10 days with only the initial login).

auth = tweepy.OAuthHandler(CONSUMER_KEY, CONSUMER_SECRET)
auth.set_access_token(ACCESS_KEY, ACCESS_SECRET)
api = tweepy.API(auth)

5. Last is the code which sends the tweet, in the TwitterIt function. As explained in step 4, I no longer do the login here, so all that is required is a call to Tweepy’s update_status function:

 def TwitterIt(u, p, message):
    #api = twitter.Api(username=u, password=p)
    #print u, p
        #status = api.PostUpdate(message)
        #print "%s just posted: %s" % (, status.text)
        print "twittered: ", message

If everything went well, your Tweet-A-Watt has got its tweet back. If you want some instant gratification (or instant disappointment if everything didn’t go well), you can add a test tweet to the startup code, and run wattcher in its debug mode (i.e. “python -d”)

if (sys.argv and len(sys.argv) > 1):
    if sys.argv[1] == "-d":
        DEBUG = True
        TwitterIt("", "", "Up and running with OAuth...")
This entry was posted in Electronics, Programming and tagged , , . Bookmark the permalink.

One Response to Surviving the Twitter OAuthcalypse with Tweet-A-Watt

  1. Pingback: Beagleboard XM and Angstrom – Getting the Big Dog to Run At Full Speed | GigaMegaBlog