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 :

Trackbacks

Use this link to trackback from your own site.

  • Denis Rhoden

    Thank you for this article! I have a question: I too created a dummy application, pointed the callback to localhost as you did but I get an error. First I am creating a Rails 3.1 application using postgresql. The browser says it can not find ‘appName_development’? It was easy enough to start from scratch so I did, without postgresql. Now inside of sqlite the error I get is ActiveRecord::ConnectionNotEstablished.

    How do I get your tutorial to work with a postgresql install? Like your tutorial I have not created any models, actions or specified routes (Omniauth has obviously done the hard work).

    Cheers,

    • http://agrawalarun.com Arun Agrawal

      You should be able to run your application with postgresql. Actually i have not written steps for creating database here. So you have to run bundle exec rake db:create. It will create a database for you and error about not find ‘appName_development’ will go away.
      Can you give a try now?

      • Denis Rhoden

        Hi Arun!

        You were absolutely right about the rake db:migrate but I am now getting ’401 Unauthorized’? 

        • http://agrawalarun.com Arun Agrawal

          Can you give a screenshot of your application on twitter somewhere so I can see. 
          It should not come actually. 
          Make sure you are using same host as you defined in the twitter app.

          • http://www.facebook.com/agoyal74 Abhishek Goyal

            I am also getting the 401 error, could u please provide info why is it coming?

          • http://agrawalarun.com Arun Agrawal

            Hey @facebook-1428816663:disqus can you post details on github as well? https://github.com/arunagw/omniauth-twitter/issues


Follow

Get every new post delivered to your Inbox

Join other followers