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:
User visits twitter_account/new when they want to link in Twitter
They get redirected to the allow/deny page on Twitter that prompts the user to log in and accept your application
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
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).