Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

manoj-seet's avatar

How can I get a user's Facebook friends using Laravel Socialite in a test environment?

Hello Laracasts community,

I'm using Laravel Socialite to handle Facebook authentication in my Laravel application, and I’m working in a test environment only. I have successfully integrated Socialite to allow users to log in with their Facebook accounts. Now, I need to fetch a list of the user's Facebook friends after they authenticate.

I've reviewed the Laravel Socialite documentation and the Facebook Graph API documentation, but I’m unsure how to proceed with obtaining the list of friends.

Facebook Permissions: I’ve made sure to request the user_friends permission during the login process.

API Calls: I’ve attempted to use the Graph API directly to fetch friends but haven't been able to integrate it smoothly with Socialite.

Here’s a snippet of the code I’m using to authenticate users:

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

public function handleProviderCallbackFacebook(Request $request) { try { $user = Socialite::driver('facebook')->stateless()->user(); $accessToken = $user->token; // Get friends $response = Http::withToken($accessToken) ->get('https://graph.facebook.com/me/friends');

        $friends = $response->json();
        dd($friends);
    } catch (\Exception $e) {
        // Log the error or return a meaningful message
        Log::error('Facebook OAuth error: ' . $e->getMessage());
        return redirect()->route('login')->with('error', 'Authentication failed.');
    }
}

How do I use Laravel Socialite to fetch a user’s list of friends from Facebook in a test environment?

Are there any specific settings or configurations needed in the Facebook Developer Console for accessing friends data in a test environment?

Can you provide an example of how to make a request to the Facebook Graph API to get friends data after authentication?

Any guidance or examples would be greatly appreciated!

0 likes
1 reply
LaryAI's avatar
Level 58

To fetch a user's Facebook friends using Laravel Socialite, you need to ensure that you have the correct permissions and make the appropriate API calls. Here’s a step-by-step guide to achieve this:

  1. Ensure Permissions: Make sure you have requested the user_friends permission during the login process. Note that Facebook's user_friends permission only returns friends who also use the app.

  2. Facebook App Review: Ensure that your app has been reviewed and approved by Facebook for the user_friends permission. Without this approval, you won't be able to access friends data.

  3. Graph API Call: Use the Facebook Graph API to fetch the friends list after obtaining the access token.

Here’s how you can modify your existing code to fetch the user's friends:

Step 1: Update the Redirect Method

Ensure you are requesting the user_friends permission when redirecting to Facebook for authentication.

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

Step 2: Handle the Callback and Fetch Friends

After the user is authenticated, use the access token to make a request to the Facebook Graph API to get the friends list.

public function handleProviderCallbackFacebook(Request $request)
{
    try {
        $user = Socialite::driver('facebook')->stateless()->user();
        $accessToken = $user->token;

        // Get friends
        $response = Http::withToken($accessToken)
            ->get('https://graph.facebook.com/me/friends');

        $friends = $response->json();
        dd($friends); // Dump and die to see the friends list

    } catch (\Exception $e) {
        // Log the error or return a meaningful message
        Log::error('Facebook OAuth error: ' . $e->getMessage());
        return redirect()->route('login')->with('error', 'Authentication failed.');
    }
}

Step 3: Facebook Developer Console Settings

Ensure that your Facebook app is properly configured:

  • Go to the Facebook Developer Console.
  • Navigate to your app and go to the App Review section.
  • Ensure that the user_friends permission is requested and approved.

Step 4: Testing in a Test Environment

For testing purposes, you can create test users in the Facebook Developer Console:

  • Go to Roles > Test Users.
  • Create test users and add them as friends to each other.
  • Use these test accounts to log in and test the friends list functionality.

Example of a Complete Controller

Here’s a complete example of a controller handling Facebook authentication and fetching friends:

By following these steps, you should be able to fetch a user's Facebook friends using Laravel Socialite in a test environment. Make sure to handle any potential errors and edge cases appropriately.

Please or to participate in this conversation.