Using Facebook API to check if user likes Facebook page or not

If we have to check that the given user likes a particular Facebook page or not, we have a Facebook API for that. This code assumes that you have the Facebook page’s ID and have got the access token of the user via Facebook login

If not, refer this article How to do Facebook login

It invokes “me/likes/PAGE_ID” endpoint of Facebook API. If user has liked the page, response will have data.

public static boolean likesPage(HttpServletRequest req,
			FacebookClient facebookClient, String pageid) {
		try {
			JsonObject data = facebookClient.fetchObject("me" + "/likes/"
					+ pageid, JsonObject.class);
			System.out.println("likesPage : " + data);
			return data.getJsonArray("data").length() != 0;
		} catch (Exception e) {
			e.printStackTrace();
			return false;
		}
	}

Uday Ogra

Connect with me at http://facebook.com/tendulkarogra and lets have some healthy discussion :)

You may also like...

Leave a Reply

Your email address will not be published. Required fields are marked *