Skip to content


Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Sample rails application using twitter oauth
tree: 279e3361d0

Fetching latest commit…

Cannot retrieve the latest commit at this time

Failed to load latest commit information.



Prepared by: paul gallagher - Repository: Demonstration:


Demonstrates the use of rails with the Twitter OAuth 1.0a API. Uses the oauth ruby gem.

Other References

  1. Twitter API documentation -

  2. OAuth gem

  3. Twitter OAuth gem (another REST API client library for Ruby - not used in this example)

Required gems

These need to be installed in addition to all standard gems required by rails:

json (1.1.6 at time of writing) oauth (0.3.5 at time of writing)

NB: for heroku deployment, these are specified in the .gems file in the root of the project

STEP-BY-STEP (how the app was created)

  1. Install oauth gem

gem install oauth

  1. create the application shell

rails rails-twitter-oauth-sample cd rails-twitter-oauth-sample rake db:create

  1. create a member scaffold

ruby script/generate scaffold member twitter_id:integer screen_name:string token:string secret:string profile_image_url:string

Member model updated to use screen_name as the key: def to_param screen_name end

  1. Prepare the database

rake db:migrate

  1. Create the oauth support in ./lib

twitter_oauth.rb Implements TwitterOauth class, which is a wrapper around the oauth gem, providing specific support for twitter. As a design principle, the TwitterOauth class logs and re-raises any errors that occur; some custom error classes are defined to suit. It includes implementations for many of the twitter api methods (but not all at this point) oauth_system.rb A controller mixin module to provide twitter oauth support in an application. Uses the TwitterOauth class for oauth functionality. Works specifically with the Member ActiveRecord class to update/verify user details. It includes wrappers for many of the twitter api methods, basically to reroute errors into the flash hash.

  1. Modify MembersController to use OAuth

# include the oauth_system mixin include OauthSystem # specify oauth to be on all user-specific actions before_filter :oauth_login_required, :except => [ :callback, :signout, :index ]

  1. Specify routes

Map members resources Hook /members/callback method to module OauthSystem.callback map.resources :members, :collection => { :callback => :get }

Hook /signout method to module OauthSystem.signout: map.signout '/signout', :controller => 'members', :action => 'signout'

For the sample app, use MembersController.index as the landing page: map.root :controller => “members”

  1. Customise views and controller methods for some basic functionality

MembersController.index a basic landing page main page for logged-in user

  1. Add rake task to demonstrate proxy-login

see lib/tasks/test.rake: demo_proxy_login task connects as the last member and exercises the API a bit to execute: rake demo_proxy_login

Something went wrong with that request. Please try again.