检查Facebook会话加载

check facebook session onload

本文关键字:加载 会话 Facebook 检查      更新时间:2023-09-26

下面我正在尝试检查页面加载时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>

首先,您需要使用 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的部分。尝试一下,让我知道它是如何进行的:)

试试这个

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');
            }
        })
    }
});