如果用户已登录facebook,则FB登录回调函数无响应

时间:2011-12-16 22:59:26

标签: php facebook facebook-login

我正在使用js连接fb。这是我的代码:

<script>
    window.fbAsyncInit = function() {
      FB.init({
        appId      : 'xxxxxxxxxxxxxxxx',
        status     : true, 
        cookie     : true,
        xfbml      : true,
        oauth      : true
      });

      FB.Event.subscribe('auth.login', function(response) {
        obj=response.authResponse;
        token=obj.accessToken;
        setCookie("access_token_fb", token, '2');

        window.location.reload();
      },true);
    };
    (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));
  </script>
用于登录的

我正在使用:

 <div class="fb-login-button" scope="email,user_checkins"  >Login with Facebook</div>
订阅功能中的

'auth.login'有一个响应处理程序,只有当用户点击fb按钮并且尚未登录到FB时才会调用它,在fb窗口中输入他/她的电子邮件地址和密码。如果用户已登录facebook,则fb对话框打开和关闭。然后它不会将控制传递给响应者。我读到某处force =“true”使其能够检测状态变化并调用回调函数。但我不知道在哪里添加力量。我认为它应该在登录div中。它是真的还是有其他方法?

谢谢你的时间。

6 个答案:

答案 0 :(得分:11)

方法1:

虽然我相信DMCS已经为这个问题提供了正确的解决方案,但这是另一种方法。将FB.login方法与自定义登录链接一起使用。

<script>
window.fbAsyncInit = function() {
    FB.init({
        appId: 'YOUR-APP-ID',
        status: true,
        cookie: true,
        xfbml: true,
        oauth: true
    });
};

function facebookLogin() {
    FB.login(function(response) {
        if (response.authResponse) {
            console.log('Authenticated!');
            // location.reload(); //or do whatever you want
        } else {
            console.log('User cancelled login or did not fully authorize.');
        }
    },
    {
        scope: 'email,user_checkins'
    });
}

(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));
</script>

身体标记:

<div id='fb-root></div><!-- required for SDK initialization -->
<a id='fb-login' href='#' onclick='facebookLogin()'>Login with Facebook</a>

当您单击该按钮时,它将在FB.login中运行回调函数。

方法2:

或者,使用FB登录按钮插件,您可以订阅auth.statusChange并检查响应中的用户状态。

FB.Event.subscribe('auth.statusChange', function(response) {
  // check for status in the response
});

请注意,如果您使用的是fb登录按钮插件,并且该用户已登录并已连接到该应用,则不会显示任何登录按钮(请在文档here中阅读)

答案 1 :(得分:5)

我建议让Facebook为你做繁重的工作。 请注意一些变化:1。指定channelUrl,2。使用FB.getLoginStatus 3.删除设置cookie。 4.改变要收听的事件(所以如果他们在不同的窗口注销,或者在其他地方将你的应用程序deauth等)

    <script>
        window.fbAsyncInit = function() {
        FB.init({
            appId      : 'xxxxxxxxxxxxxxxx',
            status     : true, 
            cookie     : true,
            xfbml      : true,
            oauth      : true,
            channelUrl : '//WWW.YOUR_DOMAIN.COM/channel.html' // Channel File
        });

        FB.getLoginStatus(function(response) {
            if (response.status === 'connected') {

                var uid = response.authResponse.userID;
                var accessToken = response.authResponse.accessToken;

                // Do something with the access token


            } else {
                // Subscribe to the event 'auth.authResponseChange' and wait for the user to autenticate
                FB.Event.subscribe('auth.authResponseChange', function(response) {
                    // nothing more needed than to reload the page
                    window.location.reload();
                },true);      

                // now dynamically show the login plugin
                $('#mydiv').show();      
            }
        });


        };
        (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));
  </script>

答案 2 :(得分:2)

当您使用status:true初始化FB SDK时,它将从用户获取登录状态并存储它。从那时开始调用FB.getLoginStatus()时,它不会访问从FB获取最新数据,而是指在FB.init()点设置的本地存储变量 - 我认为它只刷新了每20分钟一次(见https://github.com/facebook/facebook-js-sdk/commit/5b15174c7c9b9ea669499804d4a4c7d80771b036) - 但是你可以通过这样做强制刷新:

FB.getLoginStatus(function() {...}, /*FORCE*/ true);

这应该会触发您的auth.login事件,或者您也可以将为该事件定义的处理程序传递给FB.getLoginStatus

甚至可以进一步调整此功能,如:

setTimeout(function() {
    FB.getLoginStatus(function() {...}, /*FORCE*/ true);
}, 10000); // poll every 10 seconds

答案 3 :(得分:1)

从“status:true”更改为“status:false”,我相信它会做你想要的。问题是status:true会在init上进行状态检查,因此当用户单击按钮时无需更改。

在这里找到关于倒数第二条评论的答案:http://www.bubblefoundry.com/blog/2011/03/the-facebook-login-button-and-logging-into-your-webapp/

答案 4 :(得分:0)

您需要替换

FB.Event.subscribe('auth.authResponseChange', function(response){     

FB.Event.subscribe('auth.statusChange', function(response){ 

当auth.authStatusChange事件触发状态更改是否已登录或通过提供凭据登录。

感谢

答案 5 :(得分:0)

这有效:

A client error (ValidationException) occurred when calling the GetItem operation: The provided key element does not match the schema
相关问题