How to get email from facebook through Guzzle in laravel?

我怕爱的太早我们不能终老 提交于 2019-11-28 10:38:56

问题


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.


回答1:


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
]
]);



回答2:


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 the email permission, of course. And the email has to be confirmed. You can´t be 100% sure to get an email, some users use their phone number to login.

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



来源:https://stackoverflow.com/questions/34740186/how-to-get-email-from-facebook-through-guzzle-in-laravel

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!