使用 Omniauth-facebook 客户端流程解析 cookie

发布于 2024-12-23 09:44:40 字数 2874 浏览 1 评论 0原文

我有一个旧版应用程序,我一直在努力将其转换为 Omniauth,因为该应用程序目前连接到多个第三方平台,例如 Twitter 和 Facebook。事实证明,在客户端集成 Omniauth for Facebook 比从头开始构建要耗时得多。想要最后一试,希望能得到社区的见解。

我已经按照 /example Sinatra 应用程序中的omniauth-facebook 中的示例进行客户端流程,但我在日志中不断收到以下错误:

Started GET "/auth/facebook/callback" for 127.0.0.1 at Sat Dec 31 07:52:01 -0500 2011 
Started GET "/auth/failure?message=invalid_credentials" for 127.0.0.1 at Sat Dec31 07:52:04 -0500 2011

Processing by FacebookUsersController#failure as    Parameters {"message"=>"invalid_credentials"}

我能够使用另一个分支中的服务器端流程使其正常工作,客户端流程出现问题。这是相关文件。这是怎么回事?

config/initializers/omniauth.rb

Rails.application.config.middleware.use OmniAuth::Builder do
  provider :facebook, 'xxx', 'xxx', :scope => 'email', :display => 'popup'
end

config/routes.rb

  match '/auth/:provider/callback' => 'facebook_users#login_or_register_fb_user'
  match '/auth/failure' => 'facebook_users#failure'

app/views/layouts/application.haml 这实际上是通过部分包含在布局中的,这是页面上最后加载的内容。

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

    // Additional initialization code here
  };

  // Load the SDK Asynchronously
  (function(d){
     var js, id = 'facebook-jssdk'; if (d.getElementById(id)) {return;}
     js = d.createElement('script'); js.id = id; js.async = true;
     js.src = "//connect.facebook.net/en_US/all.js";
     d.getElementsByTagName('head')[0].appendChild(js);
   }(document));

   $(function() {
     $('.facebook_button').click(function(e) {
       e.preventDefault();

       FB.login(function(response) {
         if (response.authResponse) { 
           $.get('/auth/facebook/callback');
         }
       }, { scope: 'email'});


     });
   });
</script>

app/controllers/facebook_users_controller.rb

class FacebookUsersController < ApplicationController

  def login_or_register_fb_user
    respond_to do |f|
        f.js {
          MultiJson.encode(request.env)
        }
    end
  end 
  def failure
    respond_to do |f|
         f.js {
           MultiJson.encode(request.env)
         }
    end
  end
end

你知道为什么当我尝试登录 Facebook 用户时在日志中看到“/auth/failure?message=invalid_credentials”吗?我的应用程序控制器中是否需要任何东西 - 一个 before 过滤器来解析 Facebook cookie 或其他东西?我在这里不知所措。我很确定我的设置是正确的。

此应用程序的 Facebook 设置:

感谢您的支持手。

I have a legacy app which I've been working to convert to Omniauth since the app currently connects to multiple 3rd party platforms like Twitter and Facebook. Integrating Omniauth for Facebook on the client side is proving much more time consuming then building it from scratch. Want to give it one last go, hopefully with insights from the community here.

I've followed the example in omniauth-facebook in the /example Sinatra app for client-side flow, but I keep getting the following error in my logs:

Started GET "/auth/facebook/callback" for 127.0.0.1 at Sat Dec 31 07:52:01 -0500 2011 
Started GET "/auth/failure?message=invalid_credentials" for 127.0.0.1 at Sat Dec31 07:52:04 -0500 2011

Processing by FacebookUsersController#failure as    Parameters {"message"=>"invalid_credentials"}

I was able to get this to work using the server side flow in another branch, having trouble with the client side flow. Here are the relevant files. What's going on?

config/initializers/omniauth.rb

Rails.application.config.middleware.use OmniAuth::Builder do
  provider :facebook, 'xxx', 'xxx', :scope => 'email', :display => 'popup'
end

config/routes.rb

  match '/auth/:provider/callback' => 'facebook_users#login_or_register_fb_user'
  match '/auth/failure' => 'facebook_users#failure'

app/views/layouts/application.haml
This is actually included in the layout through a partial which is the last thing to load on the page.

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

    // Additional initialization code here
  };

  // Load the SDK Asynchronously
  (function(d){
     var js, id = 'facebook-jssdk'; if (d.getElementById(id)) {return;}
     js = d.createElement('script'); js.id = id; js.async = true;
     js.src = "//connect.facebook.net/en_US/all.js";
     d.getElementsByTagName('head')[0].appendChild(js);
   }(document));

   $(function() {
     $('.facebook_button').click(function(e) {
       e.preventDefault();

       FB.login(function(response) {
         if (response.authResponse) { 
           $.get('/auth/facebook/callback');
         }
       }, { scope: 'email'});


     });
   });
</script>

app/controllers/facebook_users_controller.rb

class FacebookUsersController < ApplicationController

  def login_or_register_fb_user
    respond_to do |f|
        f.js {
          MultiJson.encode(request.env)
        }
    end
  end 
  def failure
    respond_to do |f|
         f.js {
           MultiJson.encode(request.env)
         }
    end
  end
end

Any ideas why I am seeing "/auth/failure?message=invalid_credentials" in my logs when I try and log a facebook user in? Is there anything I need in my application controller - a before filter to parse the Facebook cookie or something? I'm at a loss here. I'm pretty sure my settings are correct otherwise.

Facebook settings for this app:

Thanks for the hand.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文