Ad

How To Get Email From Facebook Through Guzzle In Laravel?

- 1 answer

I am using below code to login via facebook in laravel.

Referring https://scotch.io/tutorials/token-based-authentication-for-angularjs-and-laravel-apps for token based authentication and using https://github.com/sahat/satellizer for social media integration.

$params = [
        'code' => $request->input('code'),
        'client_id' => $request->input('clientId'),
        'redirect_uri' => $request->input('redirectUri'),
        'client_secret' => 'XXXXXXXXXXXXXXXXXXX'
        /*'client_secret' => Config::get('app.facebook_secret')*/
    ];

    // Step 1. Exchange authorization code for access token.
    $accessTokenResponse = $client->request('GET', 'https://graph.facebook.com/v2.5/oauth/access_token', [
        'query' => $params
    ]);
    $accessToken = json_decode($accessTokenResponse->getBody(), true);

    // Step 2. Retrieve profile information about the current user.
    $profileResponse = $client->request('GET', 'https://graph.facebook.com/v2.5/me', [
        'query' => $accessToken
    ]);
    $profile = json_decode($profileResponse->getBody(), true);

$profile returning only fb id and user name. What changes should I do to get email from facebook.

Ad

Answer

Use below code in your step 2.

$fields = 'id,email,first_name,last_name,link,name';
$profileResponse = $client->request('GET', 'https://graph.facebook.com/v2.5/me', [
'query' => [
    'access_token' => $accessToken['access_token'],
    'fields' => $fields
]
]);
Ad
source: stackoverflow.com
Ad