简体   繁体   English

JavaScript-显示用户是否登录到Facebook

[英]javascript - show if user logged to facebook

I want to check if a user is logged in facebook (not necessarily from my site). 我想检查用户是否登录了Facebook(不一定从我的网站登录)。 when i debug my script i see that it fails to execute FB.getLoginStatus(). 当我调试脚本时,我看到它无法执行FB.getLoginStatus()。 I also tried other FB functions, but it seems it doesn't recognize any of them except for FB.init. 我也尝试了其他FB函数,但是似乎除了FB.init之外,其他函数均无法识别。 why do they fail? 他们为什么失败? any help would be appreciated 任何帮助,将不胜感激

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

EDIT 编辑

You seems to have omitted something important in your code, please, try with this, it works for me: 您似乎在代码中省略了一些重要的内容,请尝试使用此功能,它对我有用:

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

Last thing: pay attentions to "duplicate questions" in few hours 最后一件事:几个小时内注意“重复问题”

END EDIT 结束编辑

That's your code? 那是你的代码吗? This 'my_app_id' 这个“ my_app_id”

 appId      : 'my_app_id', // App ID

it's a value that fb will generate when you will create an app, you need this number before everything. 这是fb在创建应用程序时将生成的值,您需要在所有操作之前使用此数字。

If you aren't a web developer (needed to create an app), you can go here , after you join the developer team, you can create an app and get your app id. 如果您不是网络开发人员(需要创建应用程序),则可以转到此处 ,加入开发人员团队后,您可以创建一个应用程序并获取您的应用程序ID。 Enough clear? 足够清楚了吗?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM