0

我正在使用 Facebook API 来检查访问我页面的用户是否登录到 Facebook。我尝试了一切,无法提出解决方案。我想特别声明沙盒模式已关闭。我的代码就在下面;

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

    function fbLoginStatus(response) {
        console.log("Status: "+response.session);
         if(response.session) {
            //user is logged in, display profile div
            alert("Logged in");
         } else {
            //user is not logged in, display guest div
            alert("Not logged in");
         }
     }
      window.fbAsyncInit = function() {
        FB.init({
          appId      : '111111111111111',  // Channel File
          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){
            console.log("Hey");
            if(response.session){
                console.log("You are logged in");
            }
            else{
                console.log("You are not logged in");
            }
        });
        // Additional initialization code here
      };
      // Load the SDK Asynchronously
      (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 = "http://connect.facebook.net/en_US/all.js";
          ref.parentNode.insertBefore(js, ref);
        }(document));

    </script>
4

1 回答 1

1

FB.getLoginStatus 是用来检测用户是否登录FB的函数。在函数的回调中,您正在检查response.session但据我了解,此会话不是返回 JSON 对象的有效属性。您应该使用response.status。所以代码将如下所示。

   FB.getLoginStatus(function(response) {
  if (response.status === 'connected') {
    alert("LOG IN");
  } 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.
  }
 });

希望这可以帮助

考希克

于 2012-07-26T05:53:06.687 回答