Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Accessing a user's friends via the Facebook Graph API?

Has anybody successfully accessed the list of a user's friends via the Facebook Graph API?

I have written code to authenticate users on my website via Facebook's OAuth API. It works splendidly; I can access public information, likes, interests, activities, etc. I access these data points via the following URL format, where I substitute "uid" with a valid id (I use the uid in place of "me" because "me" doesn't work as advertised):

https://graph.facebook.com/uid?access_token=...
https://graph.facebook.com/uid/likes?access_token=...
https://graph.facebook.com/uid/interests?access_token=...
https://graph.facebook.com/uid/activities?access_token=...

However, accessing friends simply does not work. The url,

https://graph.facebook.com/uid/friends?access_token=...

returns the following error:

{
   "error": {
      "type": "OAuthAccessTokenException",
      "message": "An access token is required to request this resource."
   }
}

The examples on the Graph API docs all work, yet the access token generated on that page has a different format than mine. I'm not sure why. I've followed their instructions to a tee (and have it working for everything else!).

Friends are considered publicly accessible information, and I've double-checked my Facebook account permissions, so I don't think that's the problem.

like image 495
thebossman Avatar asked Jul 13 '10 19:07

thebossman


People also ask

What data can I get from Facebook Graph API?

The Graph API is the primary way to get data into and out of the Facebook platform. It's an HTTP-based API that apps can use to programmatically query data, post new stories, manage ads, upload photos, and perform a wide variety of other tasks.

How do I get friend count on Facebook API?

You need to generate token (by clicking on Get Token you will get it) with data you want from facebook API, need to select friends option to get the friend count.

How do I get a phone number from Facebook Graph API?

Phone Number Verification Code To get a phone number's ID, call to https://graph.facebook.com/ v14. 0 /{whatsapp-business-account-ID}/phone_numbers . Replace {whatsapp-business-account-ID} with the ID of the WhatsApp Business Account the phone number belongs to.


1 Answers

If a user connects to your Application you can retrieve their friends like so:

    $facebook = new Facebook(array(
                'appId' => 'xxxxxxxx',
                'secret' => 'xxxxxxx',
                'cookie' => true,
            ));

    // $session is only != null, when you have the session-cookie, that is set by facebook, after the user logs in
    $session = $facebook->getSession(); 

    // you dont get a list of friends, but a list, which contains other friendlists
    $friendsLists = $facebook->api('/me/friends');

    foreach ($friendsLists as $friends) {
      foreach ($friends as $friend) {
         // do something with the friend, but you only have id and name
         $id = $friend['id'];
         $name = $friend['name'];
      }
   }

But you only have access to id and name of the friends.

like image 59
Pascal Klein Avatar answered Nov 15 '22 07:11

Pascal Klein