Ernesto Rodriguez
Ernesto Rodriguez

Reputation: 267

How check facebook user is fan or not of my page?

I created a Facebook App and I would like to check whether the current facebook user is fan or not of my facebook page (using Javascript). This verification must be done into the facebook app.

Best regards


I was trying the code:

function isEmptyObj(obj) {
for(var prop in obj) {
    alert(prop);
    if(obj.hasOwnProperty(prop))
        return false;
}

return true;

}

window.fbAsyncInit = function() {

          FB.init({
            appId      : 'APP_ID',
            status     : true, 
            cookie     : true,
            xfbml      : true,
            oauth      : true,
          });
          var page_id = "PAGE_ID";

              FB.api('/me/likes/' + page_id,function(response) {

                if( response.data ) {
                    if( !isEmptyObj(response.data) )
                        alert('You are a fan!');
                    else
                        alert('Not a fan!');
                } else {
                    alert('ERROR!');
                }
            });


        }

Upvotes: 0

Views: 1402

Answers (1)

Lix
Lix

Reputation: 48006

In order to check the like status of a user with the JavaScript SDK, you'll need to have the user logged into your application with the user_likes permission.

The alternative (without requiring a user to login or request permissions) is to use a server side language to parse the Signed Request that Facebook passes to any application running inside Facebook. The parsed Signed Request will contain a boolen value indicating whether or not the user has liked the page. That value will only be present if the application you are using has been embedded on a page as a Page tab application.

Upvotes: 1

Related Questions