facebook
Facebook Graph API: Querying What User Likes
February 11, 2011
0

This post will show you how to retrieve a list of the user Likes (i.e.: list of Applications, Pages that the user has clicked the Like button). You need to be familiar with setting up a Facebook Application and using the JavaScript Graph API to follow this tutorial.

For an overview of the Graph API, see:
https://permadi.com/blog/2010/11/facebook-open-graph-api-authenticating-in-iframe-applications

First of all, your application 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 are limited by the user’s preference. Ie: can still query user likes as long as the user has set it to public.

In case you’re wondering where a user might set this preference, it is at: (Account->Privacy Settings->Connecting To Facebook section -> View Settings)

So when you initialize your app, you should request the permission, as such:

FB.login(onFacebookLoginStatus, {perms:'user_likes'});

Then, to retrieve the list of Likes, you can simply call

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

PS: Again, if none of the above make sense, I suggest reading my overview of the Graph API here:
https://permadi.com/blog/2010/11/facebook-open-graph-api-authenticating-in-iframe-applications

In the callbackFunction, a response object will be passed, containing an array of objects. The object is in this format:

category
id
created_time
name

To illustrate this, let’s create a callback function that simply traces out the objects in the Firebug console:

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

	function onMyLikesResponse(response)
	{
		console.log(response);
	}

The result is something like this:

You can examine the sample code in the following page to see a working example.

https://permadi.com/tutorial/facebook-js-graph-api-query-likes/index.html

If you use it on your app, you need to change the Application Id in FB.init

You should test using a user that has Liked something, otherwise the list will be empty.

You can use the technique to ask user to Like your content before the user can proceeds to an exclusive content; or to unlock another content. You can compare the object.id with the id of your content (id could be Application Id or Page Id) to do this and if your id is not on the list, then you know that user hasn’t liked your content.

Closing this discussion, here’s an example that lists the user Likes and shows the Icons of the item that user likes.

(Note: you can simply load the icon by calling

https://graph.facebook.com/id/picture

where id is the id of the content that the user has liked.

The second example is here:
https://permadi.com/tutorial/facebook-js-graph-api-query-likes/index2.html