简体   繁体   中英

javascript - show if user logged to facebook

I want to check if a user is logged in facebook (not necessarily from my site). when i debug my script i see that it fails to execute FB.getLoginStatus(). I also tried other FB functions, but it seems it doesn't recognize any of them except for 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'

 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.

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. Enough clear?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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