OAuth with OmniAuth and Twitter 6

Posted by arunagw on November 07, 2011

Hi Folks,

If you want to have OAuth in your Rails Application with twitter. OmniAuth is the best gem to use.

OmniAuth provides list of  Strategies to use many OAuth for your application. Here is the List of Strategies.

Showing here a Twitter Strategy for OmniAuth. Twitter uses the OAuth 1.0a flow, you can read about it here: https://dev.twitter.com/docs/auth/oauth

For using Twitter OAuth you have to register a Application on Twitter (https://dev.twitter.com/apps/new)

Once you done with the registration obtain the Consumer Key and Consumer Secret from the Twitter Application.

Be sure to put the callback URL in the application. Callback URL is the URL where user will land after successful authentication.

Showing an image here how to register an Application with Twitter.

 

Here showing some of the steps :

Generate a new Rails Application:

rails new TwitterAuth

Update your gemfile add omniauth-twitter gem into that

gem "omniauth-twitter"

Create a config/initializers/omniauth.rb file.
Paste your key instead of XXXX, and secret instead of YYYY

Rails.application.config.middleware.use OmniAuth::Builder do
  provider :twitter, 'XXXX', 'YYYY'
end

All Done!

Just start the server

bundle exec rails server

And hit the URL

http://localhost:3000/auth/twitter

And you should be landing on the Twitter Authorize page!

After success your app will redirect to your given callback URL with information and token!

At OmniAuth.org you can try out different -2 Strategies.

 

Useful links :


Follow

Get every new post delivered to your Inbox

Join other followers