FB.Event.subscribe('auth.login')不会每次都触发?

时间:2011-11-02 11:18:52

标签: facebook connect

<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)将不会触发!!!

3 个答案:

答案 0 :(得分:3)

我遇到了和你一样的事情,我找到了一个可能对你感兴趣的函数“getLoginStatus”。

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

但是,如果“getLoginStatus”的响应是“已连接”,则当用户已登录并已授予您应用所有权限时,您可以执行要执行的操作。

完整的脚本可能如下所示:

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

答案 1 :(得分:0)

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

FB.init()中的状态检查非常复杂,当你将Facebook评论放在页面上时会更加困难......

我刚刚测试的解决方法是拥有自己的登录按钮并使用FB.login()方法,您可以在那里挂钩回调(这会替换auth.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 });
    });
});

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

答案 2 :(得分:0)

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

希望有所帮助