Feb
11
2011

 

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:
http://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:

[c]
FB.login(onFacebookLoginStatus, {perms:'user_likes'});
[/c]

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

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

PS: Again, if none of the above make sense, I suggest reading my overview of the Graph API here:
http://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:

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

	function onMyLikesResponse(response)
	{
		console.log(response);
	}
[/c]

The result is something like this:

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

http://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:
http://permadi.com/tutorial/facebook-js-graph-api-query-likes/index2.html

Leave a Reply

*