FB.Event.subscribe('auth.login') won't trigger e

FB.Event.subscribe('auth.login') won't trigger everytime?

本文关键字:won trigger login subscribe Event FB auth      更新时间:2023-09-26
<div id="fb-root"></div>
    <script>
        window.fbAsyncInit = function() {
            FB.init({
                appId      : 'MY_APP_ID',
                status     : true,
                cookie     : true,
                xfbml      : true
            });
            FB.Event.subscribe('auth.login', function(response) {
               $.ajax({
                    url: "http://example.com/fbconnect",
                    success: function(){
                     window.location.reload();
        }
                 });
            });
        };
        (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" data-perms="email,user_birthday,user_hometown,user_location">Login with Facebook</div>

它只适用于两种情况:

  1. 当用户未登录并登录facebook时触发。
  2. 当用户登录但尚未授权应用程序时,它会触发。

第三种不工作的情况是当用户登录并且他已经被授权使用应用程序时,FB.Event.subscribe('auth.login')不会触发!!

我遇到了和你一样的事情,我发现了一个函数,"getLoginStatus",这可能是你感兴趣的。

当用户已经登录并授予应用程序所有权限时,则不需要再次登录,因此不会触发该事件。

然而,如果"getLoginStatus"的响应是"connected",当用户已经登录并授予应用程序所有权限时,你可以做你想做的事情。

完整的脚本可能看起来像这样:

<script>
  window.fbAsyncInit = function() {
    FB.init({
      appId      : 'APPID', // App ID
      channelUrl : 'CHANNEL_URL', // Channel File
      status     : true, // check login status
      cookie     : true, // enable cookies to allow the server to access the session
      xfbml      : true  // parse XFBML
    });
    FB.getLoginStatus(function(response) {
      if (response.status === 'connected') {
        // the user is logged in and connected to your
        // app, and response.authResponse supplies
        // the user's ID, a valid access token, a signed
        // request, and the time the access token 
        // and signed request each expire
        var uid = response.authResponse.userID;
        var accessToken = response.authResponse.accessToken;
        FB.api('/me', function(response) {
          if (response.name != undefined ) {
            console.log("Welcome, " + response.name);
          }         
        });
      } else if (response.status === 'not_authorized') {
        // the user is logged in to Facebook, 
        // but not connected to the app
      } else {
        // the user isn't even logged in to Facebook.
      }
    });        
    // 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));
</script>  

在这里得到了完全相同的问题,它让我发疯。不喜欢Facebook Javascript SDK atm。

FB.init()中的状态检查真的很复杂,当你在页面上放置facebook评论时,它会变得更加困难…

我刚刚测试的一个解决方案是拥有自己的登录按钮并使用FB.login()方法,在那里您可以挂钩到回调(这将取代您的验证代码)。登录事件).

window.fbAsyncInit = function () {
    FB.init({
    ...
    });
    $('#fb-login-button-custom').show().click(function () {
        var scopeList = $(this).data('scope');
        FB.login(function (response) {
            alert('FB.login callback');
            console.log(response);
            document.location.href = document.location.pathname + '?fblogin=1';
        }, { scope: scopeList });
    });
});

默认登录事件实际上只在用户授权应用程序时触发。因此,当注销用户时,您可以撤销授权。但每次登录时,他们都必须"允许"该应用。

使用FB.Event.subscribe('auth.statusChange', handleStatusChange);将通知您状态更改…

希望有所帮助