0

我想检查用户是否登录 facebook(不一定来自我的网站)。当我调试我的脚本时,我发现它无法执行 FB.getLoginStatus()。我还尝试了其他 FB 功能,但似乎除了 FB.init 之外,它无法识别其中的任何功能。他们为什么会失败?任何帮助,将不胜感激

<div id="fb-root"></div>
<script type="text/javascript" src="//connect.facebook.net/en_US/all.js">
</script>    
<script type="text/javascript">   
           window.fbAsyncInit = function() {
              FB.init({
                appId      : 'my_app_id', // App ID
                status     : true, // check login status
                cookie     : true, // enable cookies to allow the server to access the session
                xfbml      : true  // parse XFBML
              });
            };

            // Load the SDK Asynchronously
            (function(d){
              var js, id = 'facebook-jssdk'; if (d.getElementById(id)) {return;}
              js = d.createElement('script'); js.id = id; js.async = true;
              js.src = "//connect.facebook.net/en_US/all.js";
              d.getElementsByTagName('head')[0].appendChild(js);
            }(document));
        function GetFBLoginStatus() {
        debugger;
        window.fbAsyncInit()
                alert("get 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");
            }
            return true;
        }, true)
    } 
    </script>
4

1 回答 1

0

编辑

你似乎在你的代码中遗漏了一些重要的东西,请试试这个,它对我有用:

window.fbAsyncInit = function() {
  FB.init({
    appId      : '', // App ID
    status     : true, // check login status
    cookie     : true, // enable cookies to allow the server to access the session
    xfbml      : true  // parse XFBML
  });

  // Here we subscribe to the auth.authResponseChange JavaScript event
  FB.Event.subscribe('auth.authResponseChange', function(response) {
    // Here we specify what we do with the response anytime this event occurs. 
    if (response.status === 'connected') {
      // logged into the app
    alert('logged');
    } else if (response.status === 'not_authorized') {
      // In this case, the person is logged into Facebook, but not into the app
    alert('not authorized but logged into fb')
    } else {
      // not logged into Facebook
    alert('not logged');
    }
  });
  };
  // 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 = "//connect.facebook.net/en_US/all.js";
   ref.parentNode.insertBefore(js, ref);
  }(document));

最后一件事:注意几个小时内的“重复问题”

结束编辑

那是你的代码?这个'my_app_id'

 appId      : 'my_app_id', // App ID

它是当您创建应用程序时 fb 将生成的值,您需要这个数字在一切之前。

如果你不是 web 开发者(需要创建一个应用程序),你可以去这里,加入开发者团队后,你可以创建一个应用程序并获取你的应用程序 ID。够清楚吗?

于 2013-06-23T19:53:06.970 回答