How to get email from facebook via Guzzle in laravel?

I am using below code for facebook login 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);

      

$, returning only fb id and username. What changes should I make to get email from facebook.

+2


source to share


2 answers


Use the below code in 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
]
]);

      

0


source


Replace this:

'https://graph.facebook.com/v2.5/me'

      

with this:

'https://graph.facebook.com/v2.5/me?fields=name,email'

      



It's called Declarative Fields, see changelog.

Also, you need to authorize with permission email

, of course. And the letter must be confirmed. You cannot be 100% sure that you will receive an email, some users use their phone number to sign in.

You should also check this out: https://github.com/sahat/satellizer/issues/615

0


source







All Articles