With the introduction of the Facebook Graph API 2.0, access a user’s friends list was removed and limited to just friends who use the same application. However, Facebook added two new APIs to allow retrieval of Friend names (and indirectly a friend count). The two new APIs are taggable_friends
and invitable_friends
.
In order to make use of the new end-points (or even just to access the user’s friends list), your application must request the user_friends
permission. This new permission was introduced as part of Graph API 2.0.
The New Endpoints
Taggable Friends
The taggable_friends
API retrieves a list of friends that can be tagged by the user in stories. Not all friends will be displayed, but of the ones that appear, the API will return the a encrypted ID that can be used for tagging, their name and profile photo. The use of this end-point requires login review from Facebook.
Invitable Friends
The invitable_friends
API is restricted to games and provides similar functionality to taggable_friends
.
Retrieving Taggable Friends
You can retrieve taggable friends using the following code:
You can retrieve inevitable friends in a similar way if you have a Game application. Just replace the /taggable_friends
in the API call with /invitable_friends
.
Using Encrypted ID to Tag Friends
You can use the encrypted IDs from API call to then tag friends in stories (status updates or actions). You must include the place
attribute as Facebook only allows friends to be tagged at location. However, if you use a page_id
with no address, Facebook hide the location and create a normal status update.
The code above creates the following status update on Facebook:
How can I get my friend email? I’m using me/friends and I get only the name and the ID but not the email, and email permission was accepted by my friend.
It is not possible to retrieve the email addresses of friends from the API. Please read the documentation carefully for supported functionality. You can only retrieve the ID, Name and Picture for a friend – and you must use the ID to tag or invite friends as shown.
Hello, how come not all friends will be displayed? How does it know which friends to display/not display? Thanks 🙂
Not all friends are returned because of privacy settings – some friends may have disallowed tagging in status updates, so Facebook filters this out and gives you a list of friend that have allowed tagging.
Ah, that makes sense. 🙂 thank you.
Another question… where do you get the “friend id” to tag them on your status? I can’t see where $tag is in your first block of code…. So I am confused to how you managed to tag the friend: Hina Shah. On the facebook doc, the syntax used is: ‘tags’=>'[{\’tag_text\’: \’Testing\’}]’,
Thank you 🙂
If you make the API call to
taggable_friends
, it will return the IDs, Names and Profile Pictures of friends that can be tagged. You can then create a comma separate list of IDs and add them to$tags
. See the examples here.Hi, sorry the doc is a bit confusing for me… I am using /me/friends instead because I only want the list of friends who are also using the app. It returns their name and id… I want to get the id as a variable so that I can sub it into the tag parameter…. am I overcomplicating things?
You won’t be able to tag friends if you’re using the
/me/friends
API call. The new/me/taggable_friends
API call needs to be used for this.Just got told off by someone on stackoverflow…. sorry if I am asking you too many questions, like I am expecting you to do all my work for me. I don’t mean to come across like that at all…..
Not a problem. What was the question on StackOverflow?
I don’t know how to get the id from the print_r results… I get the error that I am converting an array to a string. PHP is really hard. I’m so confused now to what I am trying to do. But here’s the question. http://stackoverflow.com/questions/25018552/get-friend-id-using-facebook-api-to-tag-uploaded-photo
The person who told me off because I was asking about their code that they gave – hoping to understand it more – has deleted their answer.
I just want to let the user upload a photo and then they can see the list of friends to choose to tag in the pic. And then my app would match that friend to their id and then submit that id into the tag parameter to achieve the tag. Does that work?
Sorry, I keep coming back to you. Your tutorials are good and you’re patient and nice 🙁
Have a look at StackOverflow. I’ve provided you with a working answer, including tagging code.
I understand stdClass a lot more now. But confused, the examples online only has 1 array in which they get the key from…. but with my output I have two arrays before I get to the stdClass…
So something like this: echo $array[data] then something $array[0]->id; ??
Sorry for the spam! I got it: echo $taggable[‘data’][0]->id; I was using “array” before instead of taggable which was silly.
Dude! this just saved my project!
Thanks for posting!
can you help me ?
What do you need help with?
Poor question but hope you will help, i got class ‘FacebookRequest’ not found while copying and pasting your code above, i am logging in at another page and redirecting here where exception arises to get data using graph api
Make sure you are using Facebook PHP SDK v4.0.9 or v4.0.10, and follow this tutorial to setup the files you need to include.
Thanks Niraj for the post. Is there a way to get bigger picture of taggable friends? By default, it gives image of size 50px.
Hi Piyush,
Yes, getting a larger profile image can be achieved using the fields parameter. The below example will get a larger profile image:
i do not understand the place value, how you get the place code 195383960551614
The
195383960551614
is the ID for a Facebook Page with an address. You can replace with with the ID of your own page.do you any sample code to achieve this using javascript SDK
No sorry, but the API calls for the JavaScript SDK are the same.
Hello Niraj,
I am a non-tech person aso excuse for my simple question.
So basically what this means is that if you develop an app that is not classified as a social game, users can not invite their facebook friends unless they already downloaded and signed up for that app?
Thanks for your time!
Yes, if you don’t classify your app as a game, you cannot invite users. You will need to rely on another mechanism, like Sharing to Facebook, email invites etc.
to use the taggled friends, do I need to request a permission? If so, what should I do? Thanks.
If you visit the App Review Submission page, there is a checkbox to request Review of the taggable_friends endpoint. The screenshot of this screen is included in my article – you can clearly see the option required review.
Hi niraj,
Thanks again for the code, I have question..
How to display name using foreach..
here is my code..
$taggable = (new FacebookRequest( $session, ‘GET’, ‘/me/taggable_friends’ ))->execute()->getGraphObject()->asArray();
foreach ($taggable as $friends) {
shuffle($friends);
foreach ($friends as $friend) {
$id = $friend[‘id’];
$name = $friend[‘name’];
echo $name;
echo “”;
}
}
This is the error:
Fatal error: Cannot use object of type stdClass as array in this line foreach ($friends as $friend) {
Thanks,
Joe
The error is in your
foreach
statement. Change it to:foreach ( $taggable['data'] as $friends )
I fix my first problem..
But i now encounter new problem..
This is the error:
Fatal error: Uncaught exception ‘FacebookFacebookPermissionException’ with message ‘(#200) The user hasn’t authorized the application to perform this action’
Thanks..
It looks like you’re trying to access something without asking the user to login. Get the user to login first and then try again.
the user is already login.. i also have another problem.. my app is not displaying in facebook.. this link that i want to display in my app.. http://joehamirbalabadan.com/mosbeaubff/
Check your Facebook settings and make sure your Canvas URL is correct. Also, make sure you end the URL with a
/
. Also, make sure you have set a Namespace too.Please consider these Scenario. Graph api 1.0
We are able to get friends ids e.g. “124578992” while using me/friends in Api 1.0 we were able to perform reward system to our app user on each invitation lke below
1-UserA sent facebook invitation to UserB to install app . 2-We perform a query and store UserA id as sender id and user B id as recipient id, in our DB . 3-When User B click on Facebook notification it opens a canvas page with 2 buttons Accept or Reject 4-While accept we Detect user B id and update our Db and reward UserA with some coins
Graph api 2.0 is giving Encrypted ids which is dynamic and getting change on Every request
Please advise 1-how to perform same with Facebook api 2.0 2-Is there any other way to uniquely identify our friends which always remain static.
Thanks.
When you send a invite, facebook generated a request_id. This will contain information on who the invite was sent to. What you should do is ask User B to login to your application in the Invite and then check to see if the ID matches the Request they came from (save it in a cookie). The “From ID” won’t change as the sender would have been part of your application, so you can link it back easily (even if the TO ID changes).
Yes,we are able to get request id but it will not work if inviting more that one friends.
now only issue is that getting same request id while inviting multiple friends
Using same request id how we will differentiate user
You need to get the person accepting the request to Login first, so you can obtain their User ID. Then you can link it back to the Inviting user via the Request ID and award them coins. The Request ID will be different between different users so it will work.
Yes, you will get the same request ID when inviting multiple friends. But remember, you are only interested in who sent the request. Once the user is authenticated, you can link it back to the Inviting user to award them coins.
Hi Niraj,
I have some rush question..
What if the user is already logged in in facebook and visit my page and app..
How to get session and the user info?
Thanks,
Joe
You can use the JavaScript SDK to detect if the user is already logged in and authorised to use your application, or you would need to ask them to login in again using the PHP SDK.
Hi Niraj,
How to limit display of taggable_friends?
Example i only want to display 100 only..
Thanks,
Joe
Add
?limit=100
to the end of your API call.thanks.. i already fix this.. i use break..
Using
limit
is better, as the API will return data quicker (due to smaller response).I’ve looked around and this is the closest answer I can get, thank you very much!
Also, can I tagged friends in a comment in posts of a group?
Yes, I did search and it said only be able to tag page-id. However, I wonder about it since there are quite plenty of spam auto tag friends in group apps. If not, can you please suggest me what to do with this thing: I want to do a small tool to auto tag each group member everyday in a comment in a specific post to tell his/her job. Thanks!
Yes, tagging people in groups is possible, but they need to be a friend in order to get the taggable ID. You can make a API call to:
https://graph.facebook.com/{group_id}/feed?message=Your+message&place={some-page_id-with-no-place}&tags={comma-separated-list-of-tags}
That API calls is for tagging friends in a new post in group, right? Then how about tagging friends in a comment in a specific post in a group? One post per day in the group might be spammy, I think comments will be better. Can you help?
According to the Facebook Documentation, it’s not possible to tag people in comments.
Very helpful, thanks for posting!
Hi Niraj Shah,
This tutorial help me alot. But i can’t understand, some account get successful list friend on first time, but second time it’s can’t get this list.
And now, except me, another account facebook can’t get list friend of ‘/me/taggable_friends’. (All of them are administrator of my app).
Please help me,
Tks & Rg,
Are you authenticated each time you make the API call, and are the other administrators authenticated too? Also, you need to ask for the
user_friends
permission for the API call to work.Hi Niraj,
My scope without ‘user_friends’ :(.
I fixed it. Thanks alot.
Hello Niraj,
I use your code on test app and working fine. I want to put into cron job and post one listing at a time to my fan page.
Because I want to post to my own fan page, there is no facebook login, redirection, call back url etc.
How to get this app approved by facebook team? They are not able to test the script.
If you are using it for yourself then you don’t need to get the app approved / reviewed. The review process is only if you want the additional permissions available to end-users.
I have been reading the same answers to these facebook friends list questions saying it is not possible. But when looking at some web applications, such as https://www.cloudsponge.com/test-drive, it seems that there’s a way to retrieve the user’s friends data (ids or url) without having to go through the apps-game-must-have or taggable_friends only solution. Can someone tell me how they achieve this ? Do the app needs to have some special approval ?
If you take a close look to the mentioned-above link the scope for which the app requires permissions includes only email.
Yes, this application *appears* to be accessing friends without the needed permissions, but there is a simple reason for this. The application is still using the old Graph API (v1.0), which doesn’t have this restriction on friends (note that all friends were retrievable in Graph API v1.0, and usernames were accessible too). The app won’t work after April 30th 2015, unless it uses the new methods described in this article.
Either way, you must use the new methods described in this article if you want your application to work after April 2015.
Humm, that would explain. But I did try as well calling graph.facebook.com/v1.0/… but didn’t work neither. May be I was not including the right scope for that. Any idea on which scope-endpoints is required to make it work. I understand its going to work momentarily but would like to run some tests anyway. Thank you!
If your app was created after April 2014, you won’t have access to Graph API v1.0, so it won’t work. You’ll need to have access to a older app for v1.0 of the API to work. You won’t even need the
user_friends
permission for these older Apps – you should be able to get friend information directly…Hey, Thank you for the post…. I have a big problem to get a list of the friends who use the same app… what request I should ask in facebook API? thank you very much
You need to make a API call to
/me/friends
.sir, can you provide a servlet code to access friends using me/taggable_friends….i dont have any idea about php…….
Sorry, I don’t do Java so you’ll have to translate the PHP code yourself.
Hi Niraj,
I wanted to ask if the id of the friends returned by the API call to /me/friends remains the same across different users or does it change. Like ‘A’, ‘B’ and ‘C’ are mutual friends. Will the id differ from when ‘A’ calls the API and when ‘B’ calls the API for the friend ‘C’.
I have tested this myself, and Friend IDs will match back to the scoped User ID of the same user. If all three users have a mutual friend, their User ID will be the same across all three.
Hai Niraj,
I tried to fetch facebook friendlist with access token with php and javascript as well, in both way returns empty data array, it just list the count of my friends. how to slove this issue?Would U plz help..
Are you asking for the
user_friends
permissions as the article says?How to filter the taggable_friends? For example i only want to see close friends or female friends. Please help.
You cannot filter this list – the API endpoint doesn’t provide information on gender or who a “close friend” is.
Hi Niraj,
Can you tell me if there’s another way to retrieve user friend’s location, rather than read_stream? It seems that this particular permission will not be allowed after April 2015…
There used to be a permission called
friend_location
, but that has now been removed. The only way to retrieve friend location now is to look for tagged locations in the stream.Thank you Niraj. I believe the read_stream permission will be to most apps (except Facebook branded ones), so it seems that such features will end in a few weeks…
Hi Niraj, can you tell me how to get the amount of friends tagged on a shared post? For example I shared an article and tagged 10 friends. How do the website get that 10 amount friends tagged?
Sry for poor english
If you call the API with the ID of the Shared Post, you can see who was tagged in the “tags” object – e.g.
/{post_id}
. You can also just request the tags by doing/{post_id}/tags
.Hi. How can I use this to count the number of friends a user have?
If you make a API call to
/me/friends
, the response will contain the total number of friends undersummary->total_count
:Hi, Niraj
Thanks for the post.
This is how I get my friends name
for ($i=0; $iname;
}
I try go get ‘/me/taggable_friends?fields=id,name,picture.type(large)’, can’t get only picture url, can you help me.
The API call you are making is correct. But the picture is an object, so to get the URL, you need to do:
picture->data->url
.Offf, sorry, I get it, I’m just missed one ‘->’
So the row finaly is ‘$taggable[‘data’][$i]->picture->data->url’
Thank, you for the post and help, cheers !
With this, i can select one friend random, eventually by gendre? I have problem with this item for aproval,fb dosen’t accept for first, i will try again, with new template. Thank’s !
You can’t randomly select a friend using an API call. But if you get all the user’s friends, then you can always use PHP to pick a random friend. The API call doesn’t return Genders, so you can’t do this either.
Hello Niraj
Thank you so much for writing this code.
I want to ask something. When I’m using this me/invitable_friends?fields=id,name,picture.type(large) .
I am getting this error
Array
(
[error] => Array
(
[message] => An active access token must be used to query information about the current user.
[type] => OAuthException
[code] => 2500
)
)
Full code I’m using is.
$fb -> access_token = “access_token=” . $token;
$friends = $fb -> get(“me/invitable_friends?fields=id,name,picture.type(large)”, true);
echo ” . print_r( $friends, 1 ) . ”;
Thank You so much.
Hello Niraj,
I solved my problem myself.
Thank you again for this wonderful code .
Thank You
Hi Niraj,
How do I get a list of only male or female friends of the user?
Can I use the taggable friends call to get the friend list and then get each persons gender using the id?
You cannot filter by gender or get the friend’s gender using the taggable_friends API.
You cannot filter by gender
Hi Niraj,
thank you for this great post.
We want to build a quiz like this http://en.blobla.com/bla/who-is-in-your-love-triangle with profileimages from friends. How can we get this data? Facebook rejected our query for “taggable friends” with this comment: “In API v2.0+ you can only retrieve information on friends who also use your app. Only use Taggable Friends to tag people in relevant content. Using Taggable Friends for any other purpose won’t be approved.”.
We don’t know what to do. We would very thankful for help.
Thank you
Regards
Johannes
You will need to build in tagging functionality into your app so you can get this permission approved by Facebook. It’s the only way Facebook will approve the use of this API – annoying, I know!
To make this: http://en.blobla.com/bla/who-is-in-your-love-triangle must be working on canvas?
taggable_friends or invitable_friends information can not be used of for other purposes how to create dynamic images?
Some pages that you use this:
http://vonvon.me
http://pandacat.me/
and other!!
Greetings, I hope your answer
The
taggable_friends
API returns the profile image for friends, so you can use this API to create a dynamic image of friends. However, to get the permissions approved by Facebook, you should use the permission to also tag friends (it’s intended purpose).hi,me/friends does not return friends details.Pease help me
The
/me/friends
API call will only return friends that have the app installed. If you are not seeing any data, none of your friends have installed the app.My goal is to create a page like this: http://en.vonvon.me/quiz/1623 and get the photo and name of the user’s friends who would use the app in web page.
which permission recommended me taggable_friends or invitable_friends ??
Like I said above, you can use the
taggable_friends
permission to achieve this.i had added taggable_friends permission and i retrieve login users friend list but when i login with email id then facebook gives me list of friend but when i login with mobile no then it’s gives me blank array. so can you help me why this happend.
Sounds like a bug with Facebook. Are you trying to login with the same account each time (i.e. one using email and one using mobile number for same account)? If that’s the case, its most likely a bug. You should report it to Facebook.
Is it possible to get the id(homepage) of a friend (taggable_friend) i like to ahref his image to a link.. Do you understand?
You can’t get the friend’s ID using
taggable_friends
. It was previously allowed, but was removed due to developers abusing this.Thanks … it’s not a nice action from facebook 🙁
When I send a request from JavaScript api to get list of taggable friends. I got the response but the friends IDs are long string . What are they? How to get the original user id ?
This is the correct behaviour. Facebook no longer allows you to get the real User IDs via the API. You can use the long strings in place of the User IDs to tag friends.