1

下面我试图在页面加载时检查 facebook 的会话。并希望根据登录状态在页面上显示不同的内容。怎么了?

我想前两个检查用户连接状态。然后只在页面上显示适当的内容。

   <html xmlns="http://www.w3.org/1999/xhtml" >
<head>

</head>
<body>
<script>

window.fbAsyncInit = function() {
    // init the FB JS SDK
    FB.init({
      appId      : '1781292862',                        // App ID from the app dashboard
//      channelUrl : '//WWW.YOUR_DOMAIN.COM/channel.html', // 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
  };



window.onload(){
FB.init({
    appId  : '178812862', // Your FB app ID from www.facebook.com/developers/
    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') {

    alert("yes");
    var uid = response.authResponse.userID;
    var accessToken = response.authResponse.accessToken;
  } else if (response.status === 'not_authorized') {

  } else {
    // the user isn't logged in to Facebook.
  }
 });
</script>

</body>
</html>
4

2 回答 2

0

尝试这个

FB.login(function(response) {
    if (response.authResponse) {
        FB.api('/me', function(response) {
            id= response.id;
            if(id==undefined)
            {
            alert('I am logged out');                    
            }
            else
            {
            alert('I am logged in');
            }
        })
    }
});
于 2013-09-18T07:07:29.927 回答
0

首先,您需要使用response.status而不是response.session. 并且由于状态将是字符串(“已连接”、“not_authorized”或“未知”),因此您需要if相应地设置您的语句。以下示例来自https://developers.facebook.com/docs/reference/javascript/FB.getLoginStatus/

FB.getLoginStatus(function(response) {
  if (response.status === 'connected') {
    // the user is logged in and has authenticated 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;
  } else if (response.status === 'not_authorized') {
    // the user is logged in to Facebook, 
    // but has not authenticated your app
  } else {
    // the user isn't logged in to Facebook.
  }
 });

让我知道这是否有意义,或者如果您有任何问题:)

更新

您的脚本仍然缺少将 facebook sdk 加载到您的文档中的小闭包(这就是您收到 FB 未定义错误的原因)。尝试用以下内容替换整个脚本块:

<script>
window.fbAsyncInit = function() {
    FB.init({
        appId  : 1343434343, // Your FB app ID from www.facebook.com/developers/
        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') {
            alert("yes");
            var uid = response.authResponse.userID;
            var accessToken = response.authResponse.accessToken;
        } else if (response.status === 'not_authorized') {

        } else {
            // the user isn't logged in to Facebook.
        }
    });
};

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

(^最后一部分是加载 facebook sdk 的内容。)试试看,让我知道它是怎么回事 :)

于 2013-09-18T07:07:43.460 回答