Posting to Twitter from a Ruby on Rails App

Similar to Tuesday's Posting to Facebook post, here is how to link a model in a Ruby on Rails app with Twitter and post to Twitter. This one does the exact same thing, but of course needs to use different code because everyone implements OAuth differently. To post to Twitter, I use the OAuth gem to connect, and the Twitter Gem to do posting. Note that this uses the 1.0 version of the Twitter gem which is not yet released, so the Gemfile pulls it from Github directly.

Read The Twitter Docs, specifically the Twitter OAuth Docs.

The basic flow here for the user is the same as with Facebook:

  1. User visits twitter_account/new when they want to link in Twitter
  2. They get redirected to the allow/deny page on Twitter that prompts the user to log in and accept your application
  3. Assuming they accept, they get redirected back to callback/twitter/ with an oauth_token that we use to look up the TwitterAccount (a nice feature that Facebook doesn't do!) and the oauth_verifier
  4. The application uses the OAuth gem to sends that verifier back to Twitter to retrieve an access_token, which can be used for all future posts to Twitter to act as the user

First, some Gems: (If you're using a feedreader and not looking at this on the web, the code snippets may not show up. Please click through to the original source to see them!)

Run "bundle install" and set up the routes:

Note that this is simpler than the Facebook callback because Twitter includes something in responses that can be used to look up the record instead of requiring a unique callback URL.

Create the table in a migration, something like:

Then set up the model which takes care of generating the authorize url, validating the token, and posting:

And lastly, the controller to glue it all together:

As with the Facebook example, you'll need to add your own views, error handling, etc, but hopefully this is useful to someone. I got things close to working with the old version of the Twitter gem and had to rewrite a lot of it for the 1.0 gem (because I needed some functionality only in the 1.0 version).

nice. how about one

nice. how about one integrating with statusnet ala backpack/37signals: http://37signals.com/svn/posts/2633-backpack-journal-meets-adium

StatusNet doesn't fit too

StatusNet doesn't fit too well with the project I'm working on, but sounds like a neat idea

Whats this deals_url means..?

Whats this deals_url means..? is this s the page which include a text_field to post the twitter status..?

Thats just the landing page

Thats just the landing page controller in the application I pulled this code from.

Do you have example views

Do you have example views that were used for this app that you could share?!

No, but I might be able to

No, but I might be able to answer any questions you have here.

It's Nice

It's Nice

I was a bit late to find this

I was a bit late to find this but this was a big help in me solving a rails vs. twitter/oauth puzzler, thanks!