【问题标题】:How to get email from facebook through Guzzle in laravel?如何通过 laravel 中的 Guzzle 从 Facebook 获取电子邮件?
【发布时间】:2016-04-16 20:42:36
【问题描述】:

我在 laravel 中使用下面的代码通过 facebook 登录。

参考https://scotch.io/tutorials/token-based-authentication-for-angularjs-and-laravel-apps 进行基于令牌的身份验证,并使用https://github.com/sahat/satellizer 进行社交媒体集成。

$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 只返回 fb id 和用户名。 我应该做些什么改变才能从 Facebook 接收电子邮件。

【问题讨论】:

标签: facebook laravel guzzle


【解决方案1】:

在您的第 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】:

    替换这个:

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

    用这个:

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

    它被称为“声明性字段”,请参阅更新日志。

    当然,您还需要使用email 权限进行授权。并且必须确认电子邮件。您不能 100% 确定收到电子邮件,有些用户使用他们的电话号码登录。

    你也应该看看这个:https://github.com/sahat/satellizer/issues/615

    【讨论】:

    • 可能和 laravel/guzzle 有点不同,但是你必须用“fields”参数定义你想要返回的字段。
    • 使用 'debug' => true 请求选项在 GuzzleHttp\Client 中启用调试。输出是什么?您的请求发送到 Facebook 的确切 uri 是什么?此外,为什么不简单地使用 Facebook 的 SDK?建议您还参考上面@luschn 链接到的文档。我还建议您阅读developers.facebook.com/docs/php/gettingstarted 上提供的 Facebook 开发者文档。向下滚动并查看正在使用的 Uri。
    • 我不知道为什么人们仍然使用 php,javascript sdk 更容易处理。只是说;)
    • 我有权阅读 Facebook 开发者帐户中的用户电子邮件。我仍然无法从 facebook 收到电子邮件。尝试了很多可能性。仅获取显示名称和 ID。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-09-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-08-21
    • 1970-01-01
    相关资源
    最近更新 更多