How can I get the list of user_friends from a Facebook authorized user?

3.8k views Asked by At

I was researching about the possibility of viewing the list of friends for a certain user after the user authorizes the application to view his user_friends, but I did not quite grasp the idea, so I was wondering, is there a direct way to view the list without having to go through the graph APIs and stuff? I am using Laravel's Socialite package to login and it seems to work pretty fine and returns the default information(email, name, avatar, etc..) along with other permitted things like the birth date, location and hometown. However, I am finding it hard to view the list of photos, posts, friends and groups, etc.. even though the user permits me to.

Thanks a lot in advance!

3

There are 3 answers

2
Jesús On

You just cannot do it.

Facebook does not let you getting a list of friends, but only a list of friends that are already using your app. In the official docs for the API we can read: https://developers.facebook.com/docs/graph-api/reference/v2.3/user/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.

Even more, you cannot get the list of friends using Socialite from Laravel, so I would recommend you to use another Laravel package for that, like this one: https://github.com/SammyK/LaravelFacebookSdk

Cheers!

0
velasco622 On

This answer just expands and explains how you can use what @jayenne said to make this work.

In you redirect() function, you would make a call like:

    public function redirect() {
        return Socialite::driver('facebook')
            ->fields([
                'friends'
            ])
            ->scopes([
                'user_friends'
            ])->redirect();
    }

Above, we ask Socialite to request for the user_friends permission and store it into a field called friends. Now, in your callback, you can get the friends field inside the SocialiteUser like this:

   public function callback(SocialFacebookAccountService $service) {
        $socialiteUser = Socialite::driver('facebook')
            ->fields(['friends'])
            ->user();

        //Loop through all the facebook friends returned (who are already on your site and friends with the recently registered user)
        foreach($socialiteUser->user['friends']['data'] as $fbFriend) {
            //do something with this facebook friend here
        }
        //Most tutorials log the user in like this; you define the $service and the method
        //You can find a tutorial in the Socialite docs/github repo
        $user = $service->createOrGetUser($socialiteUser);
        auth()->login($user);
        return redirect('/home');
   }
0
jayenne On

all the above info is still correct with the exception that you can get the friends list and a lot more with socialite by adding them to the 'fields and scopes in your controller...

E.G:

$socialUser = Socialite::driver('facebook')->fields(['id', 'email', 'cover', 'name', 'first_name', 'last_name', 'age_range', 'link', 'gender', 'locale', 'picture', 'timezone', 'updated_time', 'verified', 'birthday', 'friends', 'relationship_status', 'significant_other','context','taggable_friends'])->scopes(['email','user_birthday','user_friends','user_relationships','user_relationship_details'])->user();