【问题标题】:Using Omniauth facebook and ajax使用 Omniauth facebook 和 ajax
【发布时间】:2013-08-15 10:34:21
【问题描述】:

我正在尝试允许用户通过 facebook 登录,而不是被重定向到当前页面。我目前正在使用带有omniauth-facebook 的Rails 并设计用于身份验证。我假设最好的方法是通过 ajax 一旦我通过 Javascript api 收到来自 facebook 的身份验证。但是,我不确定需要将什么传递给omniauth 的回调url 以验证身份验证。这是我目前拥有的(我暂时避免使用jquery)

:javascript

  window.fbAsyncInit = function() {

    // init the FB JS SDK
    FB.init({
      appId      : 'app-id',                           // App ID from the app dashboard
      channelUrl : '//localhost:3000/channel.html',    // Channel file for x-domain comms
      status     : true,                               // Check Facebook Login status
      cookie     : true,
      xfbml      : true                                // Look for social plugins on the page
    });

    // Additional initialization code such as adding Event Listeners goes here
    document.getElementById('facebook-login').onclick = function(event) {
      FB.login(function(response) {

        if (response.authResponse) {
          var xhr = new XMLHttpRequest();
          xhr.onreadystatechange = function() {
            console.log(xhr);
            if (xhr.readyState == 4) {
              console.log(xhr.responseText);
            }
          }
          xhr.open('GET', 'http://localhost:3000/users/auth/facebook/callback', true);
          xhr.send(null);

        } else {
          console.log("Something when horrible wrong");
        }
      }, {scope: ''});
    }
  };

  // Load the SDK asynchronously
  (function(d, s, id){
    var js, fjs = d.getElementsByTagName(s)[0];
    if (d.getElementById(id)) {return;}
    js = d.createElement(s); js.id = id;
    js.src = "//connect.facebook.net/en_US/all.js";
    fjs.parentNode.insertBefore(js, fjs);
  }(document, 'script', 'facebook-jssdk'));

我最不确定的是我是否正确调用了我自己的端点(users/auth/facebook/callback),或者我是否需要向它传递任何东西。任何帮助将不胜感激。

【问题讨论】:

    标签: ruby-on-rails facebook oauth devise omniauth


    【解决方案1】:

    对我来说,问题在于发出 GET 请求而不是 POST 请求。 改成POST,问题就解决了。

    我会把它作为评论留下,因为我不会留下更多信息来证明这个解决方案的合理性,但我是新来的(StackOverflow);我没有足够的积分来发表评论。

    不管怎样,希望对你有帮助!

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-09-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-11-05
      • 1970-01-01
      相关资源
      最近更新 更多