[英]Get user's Facebook email id with facebook login button
我为我的网站创建了一个 Facebook 登录按钮,并试图获取用户的 Facebook 电子邮件地址。 我得到的响应数组没有电子邮件地址。
这是我正在使用的代码:
require_once '../../config.php';
// added in v4.0.0
require_once 'autoload.php';
use Facebook\FacebookSession;
use Facebook\FacebookRedirectLoginHelper;
use Facebook\FacebookRequest;
use Facebook\FacebookResponse;
use Facebook\FacebookSDKException;
use Facebook\FacebookRequestException;
use Facebook\FacebookAuthorizationException;
use Facebook\GraphObject;
use Facebook\Entities\AccessToken;
use Facebook\HttpClients\FacebookCurlHttpClient;
use Facebook\HttpClients\FacebookHttpable;
// init app with app id and secret
FacebookSession::setDefaultApplication($appid,$appSecretKey );
// login helper with redirect_uri
$helper = new FacebookRedirectLoginHelper(WEBSITE_FRONTEND_URL.'/facebook/fbconfig.php' );
try
{
$session = $helper->getSessionFromRedirect();
} catch( FacebookRequestException $ex ) {
//echo " When Facebook returns an error";
} catch( Exception $ex ) {
// echo "When validation fails or other local issues";
}
// see if we have a session
if(isset($session ))
{
// graph api request for user data
$request = new FacebookRequest( $session, 'GET', '/me' );
print_r($request);
$response = $request->execute();
// get response
$graphObject = $response->getGraphObject();
print_r($graphObject);
echo $graphObject->getProperty('email');
}
else
{
$loginUrl = $helper->getLoginUrl(array('email'));
header("Location: ".$loginUrl);
}
我如何获得电子邮件地址?能够获得电子邮件地址。
如果您使用的是 Graph API 的 v2.4,则需要明确指定要从 API 返回的每个字段:
$request = new FacebookRequest( $session, 'GET', '/me?fields=id,name,email' );
在这里看看我的回答:
FB.login(function (response) {
if (response.session) {
var url = '/me?fields=name,email'; // this is what you want
FB.api(url, function (response) {
alert(response.name);
alert(response.email);
});
}
else {
alert("User did not login successfully");
}
}, { scope: 'email' }); /* perms changed to scope */
将此用作网址
var url = '/me?fields=name,email';
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.