FB登录回调函数在用户已经登录脸书时没有响应

2022-08-31 01:03:41

我正在使用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中。这是真的还是有其他方法?

谢谢你的时间。


答案 1

方法 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登录按钮插件,并且用户已经登录并连接到应用程序,则不会显示登录按钮(在此处的文档中阅读))


答案 2

我建议让Facebook为你做繁重的工作。请注意一些变化:1.指定通道URL,2.使用FB.getLoginStatus 3。删除设置 Cookie 的功能。4.更改要侦听的事件(以防他们在其他窗口中注销,或者在其他地方取消对应用程序的身份验证等)

    <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>

推荐