用于Facebook的JavaScript SDK无法获取登录状态

时间:2011-11-15 16:21:02

标签: javascript facebook-graph-api

我使用下面的代码,但它总是说没有登录(即使我已经登录了 状态)。我该如何解决这个问题?

<div id="fb-root"></div>
<script src="http://connect.facebook.net/en_US/all.js"></script>
<script>
    FB.init({appId:"my appid",
             status: true,
             cookie: true,
             logging:false,
             xfbml: true,
             oauth:true});

    FB.getLoginStatus(function(response) {
        alert ("here11");
        if (response.session) {
            alert ("already logged in");
        }
        else {
            alert ("not logged in");
        }
    });
</script>

1 个答案:

答案 0 :(得分:1)

你是如何登录的?我怀疑你要facebook.com登录,然后期望这说你已经登录了?这不行。您必须使用API在自己的域上发起会话。试试这个:

FB.login(function(response) {
    if (response.authResponse) {
        console.log('Welcome!  Fetching your information.... ');
        FB.api('/me', function(response) {
            console.log('Good to see you, ' + response.name + '.');
            FB.logout(function(response) {
              console.log('Logged out.');
            });
        });
    }
    else {
        console.log('User cancelled login or did not fully authorize.');
    }
}, {scope: 'email'});

另外,请检查the documentation