php - 无法从 Facebook 登录中检索电子邮件
问题描述
我正在使用 graph api v2.2 进行 facebook 登录,不幸的是我无法从下面的代码中检索电子邮件地址
<?php
session_start();
include_once 'Facebook/autoload.php';
$fb = new Facebook\Facebook([
'app_id' => 'xxx', // Replace {app-id} with your app id
'app_secret' => 'xxxxxx',
'default_graph_version' => 'v2.2',
]);
$helper = $fb->getRedirectLoginHelper();
try {
$accessToken = $helper->getAccessToken();
} catch(Facebook\Exceptions\FacebookResponseException $e) {
// When Graph returns an error
echo 'Graph returned an error: ' . $e->getMessage();
exit;
} catch(Facebook\Exceptions\FacebookSDKException $e) {
// When validation fails or other local issues
echo 'Facebook SDK returned an error: ' . $e->getMessage();
exit;
}
if (! isset($accessToken)) {
if ($helper->getError()) {
header('HTTP/1.0 401 Unauthorized');
echo "Error: " . $helper->getError() . "\n";
echo "Error Code: " . $helper->getErrorCode() . "\n";
echo "Error Reason: " . $helper->getErrorReason() . "\n";
echo "Error Description: " . $helper->getErrorDescription() . "\n";
} else {
header('HTTP/1.0 400 Bad Request');
echo 'Bad request';
}
exit;
}
// Logged in
echo '<h3>Access Token</h3>';
var_dump($accessToken->getValue());
// The OAuth 2.0 client handler helps us manage access tokens
$oAuth2Client = $fb->getOAuth2Client();
// Get the access token metadata from /debug_token
$tokenMetadata = $oAuth2Client->debugToken($accessToken);
echo '<h3>Metadata</h3>';
echo '<pre>';
print_r($tokenMetadata);
echo '</pre>';
// Validation (these will throw FacebookSDKException's when they fail)
$tokenMetadata->validateAppId('{app-id}'); // Replace {app-id} with your app id
// If you know the user ID this access token belongs to, you can validate it here
//$tokenMetadata->validateUserId('123');
$tokenMetadata->validateExpiration();
if (! $accessToken->isLongLived()) {
// Exchanges a short-lived access token for a long-lived one
try {
$accessToken = $oAuth2Client->getLongLivedAccessToken($accessToken);
} catch (Facebook\Exceptions\FacebookSDKException $e) {
echo "<p>Error getting long-lived access token: " . $helper->getMessage() . "</p>\n\n";
exit;
}
echo '<h3>Long-lived</h3>';
var_dump($accessToken->getValue());
}
$_SESSION['fb_access_token'] = (string) $accessToken;
$response = $fb->get('/me/?fields=email',$_SESSION['fb_access_token']);
print_r($response);
这是我得到的回应 -
Access Token
string(163) "xxx"
Metadata
Facebook\Authentication\AccessTokenMetadata Object
(
[metadata:protected] => Array
(
[app_id] => XXX
[type] => USER
[application] => XXX
[expires_at] => DateTime Object
(
[date] => 2018-08-01 11:57:44.000000
[timezone_type] => 3
[timezone] => UTC
)
[is_valid] => 1
[issued_at] => DateTime Object
(
[date] => 2018-06-02 11:57:44.000000
[timezone_type] => 3
[timezone] => UTC
)
[scopes] => Array
(
[0] => email
[1] => public_profile
)
[user_id] => XXX
)
)
但我无法获得我试图在这里寻找代码示例的电子邮件地址,但找不到适合的东西
我试图使用这个 $response = $fb->get('/me/?fields=email',$_SESSION['fb_access_token']); 为了获取数据
解决方案
您必须向用户请求此特定权限。
使用此参数在您的代码中指定它:
$permissions = ['email']; //here you set all the permisions
您必须使用此行(或等效代码,在您的代码中)发送:
$loginUrl = $helper->getLoginUrl('https://example.com/fb-callback.php', $permissions);
这个 Stack Overflow 页面似乎更详细地回答了您的问题: Facebook 登录 PHP 权限或查看官方 Facebook 文档。
推荐阅读
- angularjs - 字段标签中的角度单选按钮
- ssl - Caddy 服务器 HTTPS 重定向导致“重定向过多”
- php - PHP API 错误 - - 无法打开流:HTTP 请求失败!HTTP/1.1 401 未经授权
- python - 如何为 A 或 B 制作 discord api 等待消息?
- javascript - 如何使用 renderMessage、renderMessageImage 函数以及消息对象为空时如何显示未找到的消息
- javascript - 如何修复“未捕获的 TypeError:无法在 'URL' 上执行 'createObjectURL':未找到与提供的签名匹配的函数..”
- python - 捕获设备附加分离通知
- java - Web 服务器启动时将 JSON 文件转换为 java 对象
- javascript - 如何修复 React Native 中的导航问题(TypeError: undefined is not an object(evalating '_this.props.navigation)
- mapbox-android - Mapbox 交通插件错误