Rails: Could not authenticate you from Facebook because “Invalid credentials”

女生的网名这么多〃 提交于 2019-11-30 12:29:46

Thought I'd chip in here since this came up for me when trying to search for a solution for Could not authenticate you from Facebook because “Invalid credentials”

The problem is with Facebook API version >=2.3 you need to set {token_params: {parse: :json}} to your provider config.

devise.rb

config.omniauth :facebook,
    APP_ID,
    APP_SECRET,
    token_params: { parse: :json } # <----- this line is NB

Answer found on this issue for omniauth-oauth2

UPDATE Aug 2018: The "invalid credentials" issue reoccurred, I had to remove the token_params setting for it to work again - so this may not be an issue anymore

user2206724

Got it working!

My routes.rb and user.rb were wrong. And changed omniauth.rb too! Here are the previous and after files:

My routes.rb was:

devise_for :user, :controllers => { :registration => "registration" }
devise_for :user, :controllers => { :omniauth_callbacks => "omniauth_callbacks" }

So it was calling devise twice. I changed it to:

devise_for :user, :controllers => { :registration => "registration", :omniauth_callbacks => "omniauth_callbacks" }

Changed my omniauth.rb from this:

OmniAuth.config.logger = Rails.logger

Rails.application.config.middleware.use OmniAuth::Builder do
provider :facebook, ENV['FACEBOOK_KEY'], ENV['FACEBOOK_SECRET'], {:provider_ignores_state => true}
end

to this:

OmniAuth.config.logger = Rails.logger

Also, i defined method "def self.find_for_facebook_oauth(auth, signed_in_resource=nil)" outside user.rb model (major mistake).

So got it working perfectly now :-)

Hope this helps someone.

Windy Windi

Got it working too :) We don't need to add this code in omniauth.rb

Rails.application.config.middleware.use OmniAuth::Builder do
  provider :facebook, ENV['FACEBOOK_KEY'], ENV['FACEBOOK_SECRET']
end

if we already declare it in devise.rb

require "omniauth-facebook"
config.omniauth :facebook, "APP_ID", "APP_SECRET"

It helped me to solve a similar problem:

Note: v2.0.1 has an issue with callback url error. You need to add a callback url on config.

config.omniauth :facebook, "APP_ID", "APP_SECRET",
                callback_url: "CALLBACK_URL"

https://github.com/plataformatec/devise/wiki/OmniAuth%3a-Overview

Upgrading gem to 4.0.0 and adding require "omniauth-facebook" to devise.rb fixed this for me.

I stack with this problem and no one advice was help me. Problem was in redirect_uri. Devise omniauth gems generated it without https.

Finally resolved this by two steps:

  • Add force_ssl for rails.
  • Do not forget to add proxy_set_header X-Forwarded-Proto https; for nginx config, if you are using it.
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!