facebookfacebook-graph-apifacebook-likefacebook-pagefbml

How to check whether user already likes a page or not using FB.api?


I have used below code to check whether the user already likes the page or not.

window.fbAsyncInit = function () {
               FB.init({
                appId: 'XXXXXXXXXXX', // App ID
                channelUrl: '//' + window.location.hostname + '/channel', // Channel File
                status: true, // check login status
                cookie: true, // enable cookies to allow the server to access the session
                oauth: true,
                xfbml: true  // parse XFBML
            });

FB.getLoginStatus(function (response) {
      if (response && response.authResponse) {
         getAPIResponse();
      }
      else {
          FB.login(function (response) {
              if (response.authResponse) {
                   getAPIResponse();
              }
              else {
                document.getElementById('MainView').style.display = 'none';
                   }
          });
                }
            });
};

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

 function getAPIResponse() {
         FB.api("me/likes/XXXXXXXXXXXXX", function (response) {
                if (response.data.length == 1) {
                    document.getElementById('MainView').style.display = 'block';
                } else {
                    document.getElementById('MainView').style.display = 'none';
                    alert("Like our Page to show Calendar");
                }
            });
  }

The above code previously working correctly. But now response.data.length is always returning zero. How to resolve above mentioned issue?


Solution

  • Seems like you're not requesting the user_likes permission which is necessary to use the like info.

    See

    Furthermore, from your code example, you seem to "like-gate" content, which is not allowed by Facebook, and therefore will probably not allow you to pass the Login Review, which is necessary to use user_likes.