Feb
11
2011

 

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.

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

 

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

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

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.

[c]
	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);	
	}
[/c]

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.

Leave a Reply

*