facebook
Facebook Graph API: Checking If User Likes Your Content
February 11, 2011
0
Share this:Share on FacebookTweet about this on TwitterShare on TumblrPin on PinterestShare on Google+

To check whether a particular user likes your Page or App, you can use this Graph API call /me/likes, after the user is logged in to your application. You need the page id or application id to check against.

 

Your application also needs to have an extended permission named user_likes to retrieve a list of user Likes. Read about this permission here: http://developers.facebook.com/docs/authentication/permissions/.

Without this permission you can still query user likes but only if the user has set user_likes preference to be accessible to public.

The above setting available at: (Account->Privacy Settings->Connecting To Facebook section -> View Settings)

&nbsp

In a nutshell, here’s what you need to do:
First, call the /me/likes API.

FB.api('/me/likes', onLikeCheckResponse);

 

Then, check the response.data array and compare the data.id with your application or page id.

		for (var i=0; i<response.data.length; i++)
		{
			if (response.data[i].id=="163504083699380")
			{
				alert("You like this app.");
				return;
			}
		}

In this example, 163504083699380 is my application id. I then iterate every user Like and check their id against my application id. If my application id is found on the list, then I know that the user likes my page.

	function onLikeCheckResponse(response)
	{
		// Iterate and check against your id
		for (var i=0; i<response.data.length; i++)
		{
			if (response.data[i].id=="163504083699380")
			{
				alert("You like this app.");
				return true;
			}
		}
		alert("You didn't like this app.");
		return false;
	}

	function checkLike()
	{
		FB.api('/me/likes', onLikeCheckResponse);
	}

Example:

 

IMPORTANT UPDATE

:
As of 12/16/2011, there have been the following changes to the Facebook API:

  • response.session is now called response.authResponse. So, anywhere you see response.session, replace it with response.authResponse.
  • perms is now called scope. So, anywhere you see perms, replace it with scope.
Share this:Share on FacebookTweet about this on TwitterShare on TumblrPin on PinterestShare on Google+
Write your comment here ...

Leave a Reply