Alexis
Alexis

Reputation: 149

Rails Devise_token_auth invalid_credentials: OAuth2::Error

I'm building an api a restful based rails 5, devise_token_auth and Omniauth google Oauth2.

Currently basic account creation through email is working pretty well, Now I'm trying to configure Authentication with Google but during the authentication I'm facing to some errors that I'm not able to solve.

Below the different error I face:

Started GET "/omniauth/google_oauth2/callback?state=xxxxx&code=xxxxx" for 127.0.0.1 at 2018-04-24 20:57:55 +0800
I, [2018-04-24T20:57:55.496909 #14173]  INFO -- omniauth: (google_oauth2) Callback phase initiated.
I, [2018-04-24T20:57:56.719943 #14173]  INFO -- omniauth: (google_oauth2) Callback phase initiated.
E, [2018-04-24T20:57:57.074575 #14173] ERROR -- omniauth: (google_oauth2) Authentication failure! invalid_credentials: OAuth2::Error, invalid_grant: Code was already redeemed.
{
  "error" : "invalid_grant",
  "error_description" : "Code was already redeemed."
}
Processing by Devise::OmniauthCallbacksController#failure as HTML
  Parameters: {"state"=>"xxxxxxxx", "code"=>"xxxxxxx"}
Redirected to http://localhost:3000/auth/sign_in

Or

Started GET "/omniauth/google_oauth2/callback?state=xxxxxx&code=xxxxxx" for 127.0.0.1 at 2018-04-24 21:13:46 +0800
I, [2018-04-24T21:13:46.268484 #14173]  INFO -- omniauth: (google_oauth2) Callback phase initiated.
I, [2018-04-24T21:13:47.609079 #14173]  INFO -- omniauth: (google_oauth2) Callback phase initiated.
E, [2018-04-24T21:13:47.978029 #14173] ERROR -- omniauth: (google_oauth2) Authentication failure! invalid_credentials: OAuth2::Error, invalid_grant: Bad Request
{
  "error" : "invalid_grant",
  "error_description" : "Bad Request"
}
Processing by Devise::OmniauthCallbacksController#failure as HTML
  Parameters: {"state"=>"xxxxxx", "code"=>"xxxxxx"}
Redirected to http://localhost:3000/auth/sign_in

Here also the configuration of: devise.rb:

  config.omniauth :google_oauth2, 
                  'xxxx.apps.googleusercontent.com',   
                  'client_secret',
                  provider_ignores_state: true,
                  :access_type => 'offline'

omniauth.rb:

Rails.application.config.middleware.use OmniAuth::Builder do
    provider :google_oauth2, 
             'xxxxx.apps.googleusercontent.com',
             'client_secret',
             provider_ignores_state: true,
             :access_type => 'offline'
end

If you have any idea of the root cause, feel free to update it,

Upvotes: 0

Views: 1117

Answers (1)

Ali Asad
Ali Asad

Reputation: 1

If you are facing

OAuth2::AccessToken.from_hash: `hash` contained more than one 'token' key (["access_token", "id_token"]); using "access_token".
D, [2024-06-20T19:17:06.174097 #23685] DEBUG -- omniauth: (google_oauth2) Callback phase initiated.
E, [2024-06-20T19:17:06.341526 #23685] ERROR -- omniauth: (google_oauth2) Authentication failure! invalid_credentials: OAuth2::Error, invalid_grant: Bad Request

I was using the client_id and client_secret in both application.rb and devise.rb.

I removed the one in application.rb. That issue has been fixed.

Upvotes: 0

Related Questions