Using Facebook API from Java to find friends who like user’s photos the most

Logic behind most of the facebook apps revolve around the data mining of user’s posts. Best way is to find user’s friends who have liked his posts the most. This way apps become for interactive and user will have tendency to share such app’s result. For example ‘Which friend will get married next’ – Most interactive result would be to show user’s most interactive friend.

So how do we calculate this data. This article assumes that you have already got the access token from the user.

If you do not know,follow this article : Facebook login and getting user’s access token

We will fetch all the photos of the user and check which friend has liked most number of pics.

We will being restfb java client to invoke Facebook’s API.

1. First create Facebook client using the access token

String access_token = at; // You would be already having this token
		FacebookClient client = new DefaultFacebookClient(at);
		String userid = getUserId(req, client);

2. Next we will hit Facebook API’s ‘albums’ endpoint to fetch all user’s albums

		Map<String, Integer> data =  new HashMap<String, Integer>();
		List<JsonObject> albumdata = client.fetchConnection("me" + "/albums",
				JsonObject.class, Parameter.with("fields", "id,name"))

3. Now we will iterate over albums and fetch all photos of each album. For each photo we will be fetching its likes and we have put a limit of maximum 1000 likes

		for (int x = 0; x < albumdata.size(); x++) {
			try {
				JsonObject ajobj = albumdata.get(x);
				String albumname = ajobj.getString("name").toLowerCase();
				List<JsonObject> idata = client.fetchConnection(
						ajobj.getString("id") + "/photos", JsonObject.class,
						Parameter.with("fields", "id,likes.limit(1000)"),
						Parameter.with("offset", 0),
						Parameter.with("limit", 1000)).getData();

4. Iterate over all photos of each album and fetch their likes

				for (int xx = 0; xx < idata.size(); xx++) {
					try {
						JsonObject jobj = idata.get(xx);
						JsonObject likes = jobj.getJsonObject("likes");

5. Iterate over all the likes for each photo and and fetch the userid of the user who had liked. We also have a check that you dont count user’s own like. Maintain a map of user vs ‘his likes count’ and keep on updating it

JsonArray ary = likes.getJsonArray("data");
						for (int xxx = 0; xxx < ary.length(); xxx++) {
							JsonObject jo = ary.getJsonObject(xxx);
							String id = null;
							try {
								id = (String) jo.getString("id");
								// status = status + "333";
								if ((id + "").equals(userid + "")) {
							} catch (Throwable e) {
							updateMap(data, id);
public static void updateMap(Map<String, Integer> country, String m) {
		// TODO Auto-generated method stub
		m = m.toLowerCase();
		if (country.containsKey(m)) {
			Integer v = country.get(m);
			country.put(m, ++v);
		} else {
			country.put(m, 1);

6. Once map has been constructed, sort by the number of likes

		data = sortByValue(data, DATASIZE);
                public static <K, V extends Comparable<? super V>> Map<K, V> sortByValue(
			Map<K, V> map, int v) {
		List<Map.Entry<K, V>> list = new LinkedList<Map.Entry<K, V>>(
		Collections.sort(list, new Comparator<Map.Entry<K, V>>() {
			public int compare(Map.Entry<K, V> o1, Map.Entry<K, V> o2) {
				return (o1.getValue()).compareTo(o2.getValue());
		Map<K, V> result = new LinkedHashMap<K, V>();
		int i = 0;
		for (Map.Entry<K, V> entry : list) {
			if (i > v) {
			result.put(entry.getKey(), entry.getValue());
		return result;

7. Now you have the map of users and their likes count. Iterate over it and fetch user info for each userid.

This topic has already been covered here :

Fetch user info using Facebook’s graph API

So finally you have list of all the users who have liked given’s user’s photos most of the times

Mar Java Mit Java 🙂

Uday Ogra

Connect with me at and lets have some healthy discussion :)

You may also like...

Leave a Reply

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