Rails:无法通过Facebook验证您的身份,因为“无效凭证” [英] Rails: Could not authenticate you from Facebook because "Invalid credentials"
问题描述
我使用 https://github.com/plataformatec/整合omniauth-facebook设计/维基/ OmniAuth%3A-概述。但是我收到错误:
I integrated omniauth-facebook using https://github.com/plataformatec/devise/wiki/OmniAuth%3a-Overview. But I am getting error of :
Could not authenticate you from Facebook because "Invalid credentials".
在日志中,获取:
Authentication failure! invalid_credentials: OAuth2::Error, : {"error":{"message":"This authorization code has been used.","type":"OAuthException","code":100}}
我已经安装了设备。当我点击Facebook登录链接时,它回到设计标志www.mealnut.com/user/sign_in# = 并给出上述错误。我在 https://github.com/plataformatec上查看了无效凭证的解决方案/ devise / wiki / OmniAuth%3a-Overview ,正如我所说的那样,我的应用程序是App Type = Web的头文件。没有得到为什么它不工作。
I have devise installed. When i click on facebook sign in link, it comes back to devise sign "www.mealnut.com/user/sign_in#=" and gives above error. I checked the solution for "Invalid credentials" on https://github.com/plataformatec/devise/wiki/OmniAuth%3a-Overview and as mentioned there, my app is header set for App Type = Web. Not getting why it is not working.
另外我的应用程序正在等待从Facebook的审查。但我不认为这与这个错误有关。以下是我为omniauth-facebook做的事情:
Also my app is pending review from facebook. But i don't think it is related to this error. Following are the things i did for omniauth-facebook:
Gemfile包含:
Gemfile contains:
gem "omniauth", "~> 1.1.4"
gem 'omniauth-facebook', '1.4.1'
在用户模型中,添加:
devise :omniauthable, :omniauth_providers => [:facebook]
attr_accessible :provider, :uid
def self.find_for_facebook_oauth(auth, signed_in_resource=nil)
user = User.where(:provider => auth.provider, :uid => auth.uid).first
unless user
user = User.create(name:auth.extra.raw_info.name,
provider:auth.provider,
uid:auth.uid,
email:auth.info.email,
password:Devise.friendly_token[0,20]
)
end
user
end
devise.rb
devise.rb
require "omniauth-facebook"
config.omniauth :facebook, "APP_ID", "APP_SECRET", :scope => "offline_access, email"
omniauth.rb:
omniauth.rb:
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
route.rb:
devise_for:user,:controllers => {:omniauth_callbacks =>omniauth_callbacks}
devise_for :user, :controllers => { :omniauth_callbacks => "omniauth_callbacks" }
Omniauth控制器:
Omniauth controller:
class Users::OmniauthCallbacksController < Devise::OmniauthCallbacksController
def facebook
@user = User.find_for_facebook_oauth(request.env["omniauth.auth"], current_user)
if @user.persisted?
sign_in_and_redirect @user, :event => :authentication #this will throw if @user is not activated
set_flash_message(:notice, :success, :kind => "Facebook") if is_navigational_format?
else
session["devise.facebook_data"] = request.env["omniauth.auth"]
redirect_to new_user_registration_url
end
end
end
有人可以帮助吗?
推荐答案
想到我会在这里筹码,因为当我尝试搜索一个解决方案时,为了找到解决方案,因为无效的凭据,无法验证你的Facebook
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"
问题是与Facebook API版本> = 2.3,您需要设置 {token_params:{parse::json}}
给你的提供者配置。
The problem is with Facebook API version >=2.3 you need to set {token_params: {parse: :json}}
to your provider config.
devise.rb
devise.rb
config.omniauth :facebook,
APP_ID,
APP_SECRET,
token_params: { parse: :json } # <----- this line is NB
答案发现于此问题 omniauth-oauth2
这篇关于Rails:无法通过Facebook验证您的身份,因为“无效凭证”的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!