Migrating Ruby Twitter apps to OAuth

Migrating Twitter API apps from basic authentication to OAuth is up there with hoovering the bathroom and taking out the recycling in the “irritating chores to put off for another day” stakes. However, Twitter’s announcement that they’re dropping basic authentication support at the end of this month has made this task altogether more urgent, like an unexpected visit from the mother-in-law.

If you’re using the Ruby Twitter gem by John Nunemaker, you’ll be happy to learn that it supports OAuth. You’ll be less happy to see that the example code on the homepage merrily omits the difficult part:

# NOT SHOWN: granting access to twitter on website
# and using request token to generate access token

If your Twitter app is of the simple ‘bot’ variety, with a single dedicated account where everything happens, and no need for end users to authenticate against it, then most of the example code and documentation floating around the internet is overkill, sending you down the path of sessions and callback URLs, when all you really want to know is: what do I put in the ‘register an application’ form? How do I get hold of these tokens? And what do I do with this PIN code it’s just given me?

I eventually found a blog post at BeefyApps which cleared things up immensely, and so I’ve now reshuffled their code snippet into a mini command line utility which will spit out those all-important tokens and tell you how to use them.

twitter_oauth_setup.rb on gist.github.com

5 Responses to “Migrating Ruby Twitter apps to OAuth”

  1. Matt, you’re a star! Had been meaning to “prepare for the mother-in-law” for months. My UK Traffic Tweets are now fully OAuthificated.

  2. So that’s a weird coincidence. I came here looking for jsspeccy, but coincidentally was just porting a single-purpose, single-account tweeting Ruby script myself earlier today.

    Since I wasn’t already using the twitter gem (just a POST with HTTP Basic Authentication) then starting from scratch I found the simplest thing to do was use this rather better documented alternative gem: http://github.com/moomerman/twitter_oauth (Not much to choose between them though.)

  3. cori says:

    Man I was just beating my brains out wonder how odd it seemed to have to go through so much extra steps for oauth just for a bot. Then I found your post.

    This is a great piece of code, and it’s re-usable so it will be easy to keep coding my bots. The code worked as expected, and got my bots back online in under 5 mins.

    You are a lifesaver!! Thanks a million Matt!

  4. Christian says:

    Great job with this, but it needs to be updated as the gem doesn’t use OAuth anymore …

    Twitter.configure do |config|
    config.consumer_key = TWITTER_CONSUMER_KEY
    config.consumer_secret = TWITTER_CONSUMER_SECRET
    config.oauth_token = TWITTER_ACCESS_TOKEN
    config.oauth_token_secret = TWITTER_ACCESS_SECRET
    end

    client = Twitter::Client.new

    Thanks!

  5. [...] when this awesome command line script came to my [...]

Leave a Reply