Omniauth-facebook不断报告invalid_credentials

时间:2012-07-22 01:27:19

标签: ruby-on-rails facebook omniauth

我正在尝试按照Railscast#360中的描述实现omniauth-facebook,并遇到了相当大的障碍。当我点击登录链接时,我得到了所需的弹出窗口,要求我输入我的facebook凭据,但是当我提交时,我得到一个OmniAuth :: Strategies :: OAuth2 :: CallbackError错误。在apache日志中,打印出来:(facebook)身份验证失败! invalid_credentials:OmniAuth :: Strategies :: OAuth2 :: CallbackError,OmniAuth :: Strategies :: OAuth2 :: CallbackError

这是相关代码:

omniauth.rb

OmniAuth.config.logger = Rails.logger

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

sessions_controller.rb

class SessionsController < ApplicationController
  def create
    user = User.from_omniauth(env["omniauth.auth"])
    session[:user_id] = user.id
    redirect_to root_url
  end

  def destroy
    session[:user_id] = nil
    redirect_to root_url
  end
end

application.html.erb

<div id="fb-root"></div>
<script>        
window.fbAsyncInit = function() {
    FB.init({
        appId      : '(**my app id**)', // App ID
        status     : true, // check login status
        cookie     : true // enable cookies to allow the server to access the session
    });

    $('#sign_in').click(function(e) {
        e.preventDefault();
        return FB.login(function(response) {
            if (response.authResponse) {
                return window.location = '/auth/facebook/callback';
            }
        });
    });

    return $('#sign_out').click(function(e) {
        FB.getLoginStatus(function(response) {
            if (response.authResponse) {
                return FB.logout();
            }
        });
        return true;
    });
};
 </script>

我错过了一些简单的东西吗?过去几天我一直在寻找解决方案。

7 个答案:

答案 0 :(得分:68)

似乎omniauth-facebook v1.4.1引入了CSRF的问题。临时修复只是回滚到v1.4.0。在Gemfile中,将omniauth-facebook行更改为:

gem 'omniauth-facebook', '1.4.0'

我已经报告了这个问题:https://github.com/mkdynamic/omniauth-facebook/issues/73

答案 1 :(得分:6)

我有一个类似的问题,它为1个用户工作,但第二个用户收到了Authenticating错误。

禁用沙盒模式(应用&gt;设置&gt;高级)似乎修复了它。

答案 2 :(得分:1)

在你的omniauth.rb中添加代码:

OmniAuth.config.on_failure = Proc.new do |env| new_path = "/auth/failure"
 [302, {'Location' => new_path, 'Content-Type'=> 'text/html'}, []]
end

答案 3 :(得分:1)

我注意到omniauth-oauth2&gt; 1.0.3也会引起问题,卸载更高版本并保持omniauth-oauth2 1.0.3解决了问题..

答案 4 :(得分:0)

我也有这个。

删除application.html.erb中的JS脚本(但保持fb-root div)将起作用。无论如何,FB登录界面将不再显示在弹出窗口中,您将被重定向到FB登录,然后返回到您的站点。

答案 5 :(得分:0)

对于像我一样粗心的人,

在部署之前在developers.facebook上

Remember to switch you app out of Sandbox mode

沙盒模式将触发除开发者帐户以外的所有人的csrf错误。

答案 6 :(得分:0)

你可能想要覆盖OmniauthCallbacksController,并将其添加到logging:

class OmniauthCallbacksController < Devise::OmniauthCallbacksController
  def failure_message
    exception = env["omniauth.error"]
    #add login here:
    Rails.logger.info "exception: #{exception.inspect}"
    error   = exception.error_reason if exception.respond_to?(:error_reason)
    error ||= exception.error        if exception.respond_to?(:error)
    error ||= env["omniauth.error.type"].to_s
    error.to_s.humanize if error
  end

  #other code ...
end
在我添加了我之后,我发现&#34;无效的IP ...&#34;问题,