2

如果用户登录到 Facebook,FB.getLoginStatus() 工作正常。但是,如果用户没有登录到 facebook,则不会调用 FB.getLoginStatus() 回调,并且我无法在我的代码中处理这种情况,因为我没有收到回调。我不在沙盒模式下。

我还添加了 'true' 来强制回调,例如)FB.getLoginStatus(function(response){}, true)。它没有帮助。这是一个错误吗?还修好了吗?

如果没有,请在用户退出 Facebook 时解决。我现在在这个问题上坚持了一周。

function GetFBLoginStatus() {
    FB.getLoginStatus(function(response) {
        alert("inside call back function"); 
        if (response.status === 'connected') {
            var uid = response.authResponse.userID;
            var accessToken = response.authResponse.accessToken; 
            alert("Connected FBID = " + uid);
        } else if (response.status === 'not_authorized') { 
            alert("not_authorized"); 
        } else {
            alert("Not Logged into facebook");
        }
        return true;
    }, true)
} 
4

2 回答 2

5

您可以创建一个auth.logout事件:

FB.Event.subscribe('auth.logout', function(response) {
    alert('logged out!');
});

或者更好的解决方案,创建一个auth.authResponseChange事件:

FB.Event.subscribe('auth.authResponseChange', function(response) {
    console.log('The status of the session changed to: '+response.status);
    alert(response.status);
});

更新:

<div id="fb-root"></div>
<script>
window.fbAsyncInit = function() {
    FB.init({
        appId  : 'APP_ID',
        status : true,
        cookie : true,
        xfbml  : true,
    });

    FB.Event.subscribe('auth.login', function(response) {
        alert('logged in');
    });

    FB.Event.subscribe('auth.logout', function(response) {
        alert('logged out');
    });     
};

function GetFBLoginStatus() {
    FB.getLoginStatus( function(response) {
        console.log(response);
        if (response.status === 'connected') {
            var accessToken = response.authResponse.accessToken;
            alert(accessToken);
        } else if (response.status === 'not_authorized') {
            //login function
            alert('login first');
        } else {
            //login function
            alert('login first');
        }
    }, true);
}

//Load the SDK Asynchronous
(function(d) {
    var js, id = 'facebook-jssdk', ref = d.getElementsByTagName('script')[0];
    if (d.getElementById(id)) {return;}
    js = d.createElement('script'); js.id = id; js.async = true;
    js.src = "//connect.facebook.net/en_US/all.js";
    ref.parentNode.insertBefore(js, ref);
} (document));
</script>

<div>
<a href="#" onclick="GetFBLoginStatus()"><img src="../FacebookSmall.bmp" />ddd</a>
</div>
于 2012-07-02T16:22:32.040 回答
0

回应菲利普的评论:我在退出facebook后尝试刷新页面。这是我的代码:我在单击链接时调用 javascript 函数 GetFBLoginStatus()。当我登录 Facebook 时,一切正常。当我没有登录 facebook 时,我没有收到任何警报。

<div id="fb-root">
</div>

<!-- pull down Facebook's Javascript SDK -->
<script src="http://connect.facebook.net/en_US/all.js"></script>

<script>
    // initialize the library with your Facebook API key
    FB.init({ apiKey: 'XXXXXXXXXXXXX' });
</script>

<div>
<a href="#" onclick="GetFBLoginStatus()"><img src="../FacebookSmall.bmp" /></a>
</div>

<script type="text/javascript" language="javascript">
    function GetFBLoginStatus() 
{
          FB.Event.subscribe('auth.authResponseChange', function(response){                 
        alert(response.status);
        });

        FB.getLoginStatus(function(response) 
{
         alert("inside call back function");

        if (response.status === 'connected')                     
        {   
    var uid = response.authResponse.userID;
           var accessToken = response.authResponse.accessToken;                
           alert("Connected FBID = " + uid);
        } 
        else if (response.status === 'not_authorized') 
 { 
   alert("not_authorized"); 
 } 
       else 
{
  alert("Not Logged into facebook");    
}

     }, true) ;      
  }

于 2012-07-03T13:06:58.087 回答