How to get the number or list of friends by Facebook API v2.0.

8.7
2014
friend_1

from official FAQ by Facebook

There are some critical spec changes about facebook API version was changed to v2.0.
For example, if you need the permission(publish_actions) for posting from app you must be reviewed by facebook.

There is one more important spec change, the API about user’s friend was changed or deprecated.
Now, you cannot get the whole friends list of facebook user by the app was created after April 30th 2014.
The app was created before April 30th 2014 will be the same situation after April 30th 2015.

*In fact, it can use still. But it is only for “tag friends function”.

By the way, you can get the number of user’s friend.
Here is how to do this.

This spec change about user friends was hot topic in Stack Overflow.

Facebook Graph Api v2.0 me/friends returns empty, or only friends who also use my app
http://stackoverflow.com/questions/23417356/facebook-graph-api-v2-0-me-friends-returns-empty-or-only-friends-who-also-use-m

So, facebook is explaining about this at many places.

FAQ: API v2.0 and the New Facebook Login
https://developers.facebook.com/docs/apps/faq

Facebook Platform Upgrade Guide
https://developers.facebook.com/docs/apps/upgrading#upgrading_v2_0_user_ids

Re-check

The facebook API v1.0 will be completely migrated to v2.0 on April 30th 2015.
The app was created after April 30th 2014 has been already migrated to v2.0.
If it was created before April 30th 2014 it is still v1.0.

The app was created before April 30th 2014 would be working until 1 year after, but the one was created before will not be working unless you meet spec changes.

How to get the number or list of friends by old Facebook API.

If you called the “/me/friends” API you could get the whole list of users friends by v1.0.
You didn’t need any permissions that need to be reviewed by facebook.
And there was no such API that was able to get the number of friends.
So, you were able to get that number by counting the friend list.

You can check this by Graph API Explorer
Select one of the app you have, get the access token with “public_profile”, and run the “/v1.0/me/friends”, then you can get whole list of your friends.

friend3

How to get the number or list of friends by Facebook API v2.0.

The spec about “/me/friends” API is changed by v2.0.

First, you need to get the “user_friends” permission from app user.
This permission doesn’t need facebook review, and you have this scope by default.
But user can deny this permission. You may note this.

Second, there is big spec change.
It’s not “whole list of friends” but “list of the app user in friends” that you can get by calling “/me/friends” API.
So, there is no user in some cases.

You can check this by Graph API Explorer, too.
Select one of the app you have, get the access token with “user_friends”, and run the “/v2.0/me/friends”, then you can get the list of the app user in friends.(It’s not whole list of friends.)

friend4

If all your friends don’t use the same app you can see no user in the list.

See the bottom of the above image.

"summary": {
  "total_count": 194
}

Wow, it seems that the number of friends is here.
So we can use this.
Let’s see the demo.

“get the number of friends” demo

If you log in here you will see the number of your friends.
*You need to allow the permission when login.

Here is the script codes of demo page.

<script type="text/javascript">
window.fbAsyncInit = function() {
	FB.init({
		appId   : '*********',
		xfbml   : true,
		version : 'v2.0',
		status  : true,
		cookie  : true,
		oauth   : true
	});
};

function btnClick() {
	FB.login(function (response) {
		if (response.authResponse) {
			var body = 'Reading JS SDK documentation';

			FB.api('/me/friends', function(response) {
				if (!response || response.error) {
					var read_err_text = 'Error';
					$("#read_result").text(read_err_text);
				} else {
					var read_ok_text = response.summary.total_count;
					$("#read_result").text(read_ok_text);
				}
			});

		} else {
			alert('User is logged out');
		}
	}, {scope: 'public_profile,user_friends'});
};

</script>

How can we get the whole list of friends by v2.0 API.

We have two ways about it, but both of them are for limited use.

1. If you want to provide user “tag friends function”.

You can get the whole list of friends that can be tagged, by “/me/taggable_friends” API.
(*Note that someone of your friends may deny to be tagged himself. )

Graph API /user/taggable_friends
https://developers.facebook.com/docs/graph-api/reference/v2.0/user/taggable_friends

You also need the “user_friends” permission for “/me/taggable_friends” API.
You don’t need the review of “user_friends” permission, but you need the review of “Taggable Friends” function.
It is necessary to be reviewed if you want to provide user “tag friends function” in the app.
*This is not the permission review but the function review.

So, you may get the list by “/me/taggable_friends” API.
Let’s try this.

“get the list of friends” demo

I have been able to get the whole list.
Here is the script codes of demo page.

<script type="text/javascript">
window.fbAsyncInit = function() {
	FB.init({
		appId   : '*********',
		xfbml   : true,
		version : 'v2.0',
		status  : true,
		cookie  : true,
		oauth   : true
	});2
};

function btnClick() {
	FB.login(function (response) {
		if (response.authResponse) {
			var body = 'Reading JS SDK documentation';

			FB.api('/me/taggable_friends?fields=name', function(response) {
				if (!response || response.error) {
					var read_err_text = 'Error';
					$("#read_result").text(read_err_text);
				} else {
					var read_ok_text = jsonParser(response);
					$("#read_result").html(read_ok_text);
				}
			});

		} else {
			alert('User is logged out');
		}
	}, {scope: 'public_profile,user_friends'});
};

function jsonParser(response) {
    var message = "";
    var dataArray = response.data;
    for(var count in dataArray){
        message = message + dataArray[count].name;
        message = message + '<br/>';
    }
    return message;
};

</script>

*Additional note!
I’m sorry, if you are not reviewed “Taggable Friends” API can only be used by “Administrators” “Developers” “Testers” of app.
You need to be reviewed that all user can use this api.
So, the demo above doesn’t work… sorry about that.

2. If you want to provide user “invite friends function”. *Only for game.

If you want to provide user “invite friends” function for game app you can use “/me/invitable_friends” API.

Graph API /user/invitable_friends
https://developers.facebook.com/docs/graph-api/reference/v2.0/user/invitable_friends

You can get the list of friends that can be invited, by this API.
Unfortunately, this api can use just only for the game.

“/me/friendlists” API is mistakable!

There is the “/me/friendlists” API in the api document.
This “friendlists” is not the list of friends.

In facebook, You can classify your friends into some group, like “Close friends” or “Family” and more.
This group is called “friendlist”.

So, you can get this data by the “/me/friendlists” API.