0

我正在使用https://github.com/sahat/satellizer在我的 angular + laravel ( 5.2 ) 应用程序中添加带有 facebook 功能的登录。

这是我在 php 中用来调用 facebook graph api 的代码

 $client = new GuzzleHttp\Client();

    $params = [
        'code' => $request->input('code'),
        'client_id' => $request->input('clientId'),
        'redirect_uri' => $request->input('redirectUri'),
        '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); 

但它只是在响应中返回 id 和 name。我尝试将 id、name 和 email 作为 url 中的字段添加为

 $profileResponse = $client->request('GET', 'https://graph.facebook.com/v2.5/me?fields=id,name,email', [
        'query' => $accessToken
    ]);

它仍然只返回 id 和 name。

我还在 Graph API Explorer 中使用上述请求返回的令牌进行了尝试。它显示了适当的反应。

在此处输入图像描述

谢谢

4

3 回答 3

0

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

正如您在屏幕截图中看到的,您正在 API Explorer 中使用“Graph API Explorer”应用程序。切换到您自己的应用程序并使用电子邮件权限进行授权。

这是讨论的地方:https ://github.com/sahat/satellizer/issues/615

于 2016-01-12T13:07:57.017 回答
0

只有在用户授予“电子邮件”权限后,才能在用户对象上访问“电子邮件”字段。在此处输入图像描述

于 2016-01-12T13:17:29.467 回答
0

在这里创建了一个问题https://github.com/sahat/satellizer/issues/701,经过包作者的调查,guzzel 请求有问题

作者已回复此修复程序。用下面的代码替换请求。

$fields = 'id,email,first_name,last_name,link,name,picture';
$profileResponse = $client->request('GET', 'https://graph.facebook.com/v2.5/me', [
'query' => [
    'access_token' => $accessToken['access_token'],
    'fields' => $fields
]
]);
于 2016-01-14T07:24:46.710 回答