FB.Event.subscribe auth.login未被触发

时间:2013-04-09 13:49:14

标签: javascript facebook facebook-javascript-sdk

我正在使用facebook javascript sdk连接到facebook

但FB.Event.subscribe auth.login未被触发。

这是我正在使用的代码

    window.fbAsyncInit = function() {
    // init the FB JS SDK
    FB.init({
      appId      : 'xxxxxxxxx',                        // App ID from the app dashboard
      channelUrl : 'MychannelUrl', // Channel file for x-domain comms
      status     : true,                                 // Check Facebook Login status
      xfbml      : true                                  // Look for social plugins on the page
    });

    // Additional initialization code such as adding Event Listeners goes here
FB.Event.subscribe('auth.login', function(response) {console.log('event fired');
    if(response.status === 'connected') { console.log('connected');  }  
});

    }

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

1 个答案:

答案 0 :(得分:4)

使用auth.statusChange

而不是使用auth.login
FB.Event.subscribe('auth.statusChange', function(response) {
        if (response.status === 'connected') {
                      //the user is logged and has granted permissions
        } else if (response.status === 'not_authorized') {
              //ask for permissions
        } else {
              //ask the user to login to facebook
        }
    });

请记住,只有在发生用户状态更改时才会触发此操作,因此,如果要在第一次运行时获取用户的状态,请使用FB.getLoginStatus()功能。此函数给出的响应与auth.statusChange事件,connectednot_authorized

相同