0

我想讓我的用戶使用Facebook登錄到rails應用程序。這裏是 「簽了FB鏈接」:facebook omniauth登錄與rails/devise不工作,因爲錯誤驗證驗證碼

<%= link_to "Sign in with FB", omniauth_authorize_path(resource_name, provider) %> 

這會生成以下HTML:

<a href="https://stackoverflow.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://stackoverflow.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'] 
+0

這個錯誤來自Facebook的。你確定你嘗試登錄的主機是否被添加到Facebook應用配置中? –

+0

你在facebook dev中配置了一個應用程序嗎?你在設計配置中配置了APP_KEY和APP_ID嗎? https://github.com/plataformatec/devise/wiki/OmniAuth:-Overview – lcguida

+0

是的,我在devise.rb文件中有這樣一行:'config.omniauth:facebook,ENV ['APP_KEY'],ENV ['FACEBOOK_SECRET_ID 「]' – Philip7899

回答