我希望在使用他们的PHP SDK V5从Facebook登录后获取用户的电子邮件地址,但遇到了我不太了解的问题。
根据他们的分步指南https://developers.facebook.com/docs/reference/php/5.0.0
我创建了Login.php和Login-callback.php文件
的login.php
session_start() ;
require_once __DIR__ . '/src/Facebook/autoload.php';
$fb = new Facebook\Facebook([
'app_id' => $app_id,
'app_secret' => $app_secret,
'default_graph_version' => 'v2.2',
]);
$helper = $fb->getRedirectLoginHelper();
$permissions = ['email', 'public_profile']; // optional
$loginUrl = $helper->getLoginUrl('http://www.example.com/login-callback.php', $permissions);
echo '<a href="' . $loginUrl . '">Log in with Facebook!</a>';
登录-callback.php
session_start() ;
require_once __DIR__ . '/src/Facebook/autoload.php';
# login-callback.php
$fb = new Facebook\Facebook([
'app_id' => $app_id,
'app_secret' => $app_secret,
'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)) {
// Logged in!
$_SESSION['facebook_access_token'] = (string) $accessToken;
// OAuth 2.0 client handler
$oAuth2Client = $fb->getOAuth2Client();
// Exchanges a short-lived access token for a long-lived one
$longLivedAccessToken = $oAuth2Client->getLongLivedAccessToken($_SESSION['facebook_access_token']);
// Sets the default fallback access token so we don't have to pass it to each request
$fb->setDefaultAccessToken($longLivedAccessToken);
try {
$response = $fb->get('/me');
$userNode = $response->getGraphUser();
} 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;
}
echo 'Logged in as ' . $userNode->getName();
$graphObject = $response->getGraphObject();
$email = $graphObject->getProperty('email'); // This is not getting any thing
echo $email; // Empty
}
graphObject-&gt; getProperty(&#39; email&#39;);没有返回任何东西
答案 0 :(得分:7)
我的问题是一样的。我的网站认可登录,我只收到姓名和身份证。
我在这里看到了这个提示:
$fb->get('/me?fields=email');
这解决了我的问题!
答案 1 :(得分:0)
你也可以在你的php文件中使用Facebook自己的Login Button(通过他们的工具插件生成器生成),这会更有吸引力,但请记住要处理这个HTML5属性数据范围。 检查此示例:
{
auto& _1 = std::placeholders::_1;
auto f = std::bind(&Foo::bar, b, _1);
...
}
答案 2 :(得分:0)
我认为您应该使用Facebook Graph API方法获取用户帐户信息。获得成功访问令牌后,使用api graph方法。像那样:
$userProfile = $facebook->api('/me?fields=id,first_name,last_name,email,gender,locale,picture');
$userClass = new User;
$userData = $userClass->checkFBUserData($userProfile);
您可以打印这样的电子邮件地址:echo $userData['email'];
有关详细信息,请查看this tutorial
这是工作来源,我已经测试了它