Kiran
Kiran

Reputation: 5526

google oauth fails with 401 unauthorized in rails omniauth

I am trying to authenticate with google oauth2 using devise and omniauth. I set up the call backs religiously as specified in omniauth documentation and am even using the same exact code. https://github.com/plataformatec/devise/wiki/OmniAuth:-Overview

I uploaded the code to github: https://github.com/kiranjulapalli/devisetest

I get redirected to login and when I select my account, I get redirected to sign_in page again. What am I missing here?

Here is the log:

Started GET "/users/auth/google_oauth2/callback?state=3baa394efe9c586b30fgfc05b1f618af04b7728d0d75c3c1&code=4/HxV9O
37jKYgxg10EWDntdfewZTz3.4kqevOMEweMTshQV0ieZDArQUdYefAI" for 127.0.0.1 at 2013-04-15 16:36:01 -0400
Processing by Users::OmniauthCallbacksController#google_oauth2 as HTML
  Parameters: {"state"=>"3baa394efe9c586b30fgfc05b1f618af04b7728d0d75c3c1", "code"=>"4/HxV9O
37jKYgxg10EWDntdfewZTz3.4kqevOMEweMTshQV0ieZDArQUdYefAI"}
  MOPED: 127.0.0.1:27017 COMMAND      database=admin command={:ismaster=>1} (2.0001ms)
  MOPED: 127.0.0.1:27017 QUERY        database=devisetest_development collection=users selector={"$query"=>{"email"
=>"[email protected]"}, "$orderby"=>{:_id=>1}} flags=[:slave_ok] limit=-1 skip=0 batch_size=nil fields=nil
(0.9999ms)
Completed 401 Unauthorized in 24ms
#custom log statements
logger::Find if this user exists
logger::Return user
logger:Got call back
logger:User persisted

My routes.rb file:

Devisetest::Application.routes.draw do
  devise_for :users, :controllers => { :omniauth_callbacks => "users/omniauth_callbacks" }
  resources :users
  root :to => 'home#index'
end

Upvotes: 1

Views: 1215

Answers (1)

Shawn Balestracci
Shawn Balestracci

Reputation: 7530

You require your users to confirm via email before they can log on. (From the :confirmable argument to devise in your user.rb)

You could remove :confirmable or manually assign a value (such as Time.now) to the user.confirmed_at via the rails console.

You should also display the flash[:notice] in the layout somewhere, you would have seen: "Successfully authenticated from Google account. You have to confirm your account before continuing. "

It looks like: Always getting 401 Unauthorized with new install of Rails + Devise is a similar question.

Upvotes: 3

Related Questions