Jacob
Jacob

Reputation: 3698

Getting facebook friends list

I made link of facebook login which requests an access token with permission to friends list:

<a href="https://www.facebook.com/dialog/oauth?response_type=token&scope=user_friends...>Login</a>

After the user passed the login successfully and accept these permissions and my app stored the access token, I tried to get his friends list by:

https://graph.facebook.com/me/friends?access_token={ACCESS_TOKEN}

But all I get is:

{
   "data": [

   ],
   "summary": {
      "total_count": 245
   }
}

Maybe I should request something else in the scope?

Thank you !

Upvotes: 0

Views: 1925

Answers (1)

Eimantas Gabrielius
Eimantas Gabrielius

Reputation: 1386

$request = new FacebookRequest(
$session,
'GET',
'/me/friends'
);
$response = $request->execute();
$graphObject = $response->getGraphObject();

For example in CodeIgniter I use fb library and i get friends list like this:

public function get_user_friends() {
    if ( $this->session ) {
        try {
            $fr = new FacebookRequest( $this->session, 'GET', '/me/friends' );
            $request = $fr->execute();
            $user_friends = $request->getGraphObject()->asArray();

            return $user_friends;

        } catch(FacebookRequestException $e) {
            return false;

            /*echo "Exception occured, code: " . $e->getCode();
            echo " with message: " . $e->getMessage();*/
        }
    }
}

And dont forget that it has some conditions: Permissions

A user access token with user_friends permission is required to view the current person's friends.

This will only return any friends who have used (via Facebook Login) the app making the request.

If a friend of the person declines the user_friends permission, that friend will not show up in the friend list for this person.

More about: https://developers.facebook.com/docs/graph-api/reference/v2.2/user/friends

Upvotes: 1

Related Questions