facebook omniauth登录与rails/devise不工作,因为错误验证验证码
问题描述:
我想让我的用户使用Facebook登录到rails应用程序。这里是 “签了FB链接”:facebook omniauth登录与rails/devise不工作,因为错误验证验证码
<%= link_to "Sign in with FB", omniauth_authorize_path(resource_name, provider) %>
这会生成以下HTML:
<a href="https://*.com/users/auth/facebook">Sign in with Facebook</a>
这应该送我到这个控制器:
class Users::OmniauthCallbacksController < Devise::OmniauthCallbacksController
before_action :test_if_in_right_controller
def facebook
puts "in the fb controller"
# Attempt to find the User
@user = User.find_for_facebook_oauth(request.env["omniauth.auth"], current_user)
puts "1. in fb. user is #{@user.inspect}"
if @user.persisted?
puts "2. 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
puts "3. in else statement. user did not persist."
#session["devise.facebook_data"] = request.env["omniauth.auth"]
redirect_to new_user_registration_url
end
end
def passthru
puts "in the passthru"
render :file => "#{Rails.root}/public/404.html", :status => 404, :layout => false
end
def test_if_in_right_controller
puts "in the before action"
end
end
继承人的问题:
before_action正在工作,因为puts "in the before action"
行出现在日志中。但是然后控制器中的其他puts
都没有工作。什么是真正奇怪的是,Facebook的实际被接触,因为我得到以下错误:
E, [2015-11-04T08:44:36.357124 #1049] ERROR -- omniauth: (facebook) Authentication failure! invalid_credentials: OAuth2::Error, :
{"error":{"message":"Error validating verification code. Please make sure your redirect_uri is identical to the one you used in the OAuth dialog request","type":"OAuthException","code":100,"fbtrace_id":"DAgHQ3MuN59"}}
下面是完整的服务器日志
Started GET "https://*.com/users/auth/facebook/callback?code=AQA-MlSTW1tu9NuiaRa73XpD2nJKOgEmdFjFq3dZMC0m48Eep04dbql-2I80Up9CBYTAD_D7NRG_sVLVjMWRlvDtWirDhZuv1XdVZQti3I-ljSYSoiXhcYsleIZnK9osNkuYAFhF40mqM44-fZAiAGME2ny2ISj0XQIw4vvJvQDUkhS1W1jssMmCJfJM1gLbsyjgz3X3kgMRMS6_i-DTreS-lQLamfL5Kv3ElUenLTlZsmNwb3aTzcFtIV6X_5-c5XrVEs-VMmNdtEPiUFccAa6aOfwzPeP_iEyWz2F38KlKgCpOVapLVzH_fjx_LN0XOTRwJxreDe66gDP1MbgRhGMiahxQaaGEDxgxDYy3bxKW-g&state=3de1318e2031f74043cb6bd387b1eda3b3ba244d749993c5" for 127.0.0.1 at 2015-11-04 08:44:36 -0500
I, [2015-11-04T08:44:36.141145 #1049] INFO -- omniauth: (facebook) Callback phase initiated.
E, [2015-11-04T08:44:36.357124 #1049] ERROR -- omniauth: (facebook) Authentication failure! invalid_credentials: OAuth2::Error, :
{"error":{"message":"Error validating verification code. Please make sure your redirect_uri is identical to the one you used in the OAuth dialog request","type":"OAuthException","code":100,"fbtrace_id":"DAgHQ3MuN59"}}
Processing by Users::OmniauthCallbacksController#failure as HTML
Parameters: {"code"=>"AQA-MlSTW1tu9NuiaRa73XpD2nJKOgEmdFjFq3dZMC0m48Eep04dbql-2I80Up9CBYTAD_D7NRG_sVLVjMWRlvDtWirDhZuv1XdVZQti3I-ljSYSoiXhcYsleIZnK9osNkuYAFhF40mqM44-fZAiAGME2ny2ISj0XQIw4vvJvQDUkhS1W1jssMmCJfJM1gLbsyjgz3X3kgMRMS6_i-DTreS-lQLamfL5Kv3ElUenLTlZsmNwb3aTzcFtIV6X_5-c5XrVEs-VMmNdtEPiUFccAa6aOfwzPeP_iEyWz2F38KlKgCpOVapLVzH_fjx_LN0XOTRwJxreDe66gDP1MbgRhGMiahxQaaGEDxgxDYy3bxKW-g", "state"=>"3de1318e2031f74043cb6bd387b1eda3b3ba244d749993c5"}
in the before action
Redirected to http://localhost:3000/login
Completed 302 Found in 1ms (ActiveRecord: 0.0ms)
下面是从Facebook的方法User.find_for_facebook_oauth
方法控制器
def self.find_for_facebook_oauth(auth, signed_in_resource=nil)
puts "in the fb model method".green
user = User.where(provider: auth.provider, uid: auth.uid).first
puts "1. user is #{user.inspect}".green
# The User was found in our database
return user if user
# Check if the User is already registered without Facebook
user = User.where(email: auth.info.email).first
puts "2. user is already registered without fb. #{user.inspect}".green
return user if user
# The User was not found and we need to create them
new_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])
puts "the new user is #{new_user.inspect}".green
end
在我的骗局图/ intitializers/devise.rb文件,我有:
config.omniauth :facebook, ENV['APP_KEY'], ENV['FACEBOOK_SECRET_ID']
答
假设你正在测试在本地计算机上,在developers.facebook.com门户网站,在您的应用程序设置 - >高级,你应该有一个白名单您的回调:
这个错误来自Facebook的。你确定你尝试登录的主机是否被添加到Facebook应用配置中? –
你在facebook dev中配置了一个应用程序吗?你在设计配置中配置了APP_KEY和APP_ID吗? https://github.com/plataformatec/devise/wiki/OmniAuth:-Overview – lcguida
是的,我在devise.rb文件中有这样一行:'config.omniauth:facebook,ENV ['APP_KEY'],ENV ['FACEBOOK_SECRET_ID “]' – Philip7899