我想访问用户的电子邮件id,为此我尝试了下面的代码,为此我检查了许多网站。我找到了这些方法来访问用户的电子邮件id。但这些方法似乎都不起作用。请帮我解决这个问题。
提前感谢。
<?php
session_start();
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('MY_app_id', 'my_app_secret');
$helper = new FacebookRedirectLoginHelper('http://prov.resolv.com/provision_sid/Facebook_2/fbconfig.php');
try {
$session = $helper->getSessionFromRedirect();
} catch (FacebookRequestException $ex) {
} catch (Exception $ex) {
}
if (isset($session)) {
// graph api request for user data
$request = new FacebookRequest($session, 'GET', '/me');
$response = $request->execute();
// get response
$graphObject = $response->getGraphObject();
$fbid = $graphObject->getProperty('id'); // To Get Facebook ID
$fbfullname = $graphObject->getProperty('name'); // To Get Facebook full name
echo $femail = $graphObject->getProperty('email');
die();
$_SESSION['FBID'] = $fbid;
$_SESSION['FULLNAME'] = $fbfullname;
$_SESSION['EMAIL'] = $femail;
/* ---- header location after session ---- */
header("Location: index.php");
} else {
$loginUrl = $helper->getLoginUrl();
header("Location: " . $loginUrl);
}
?>
I tried multiple option to request email id
1> $request = new FacebookRequest($session, 'GET', '/me?fields=name,email');
2> $request = new FacebookRequest($session, 'GET', '/me',['fields' => 'id,name,email,address,first_name,last_name']);
通过上述请求,我无法获得用户的电子邮件id。
是否有其他方法可以请求用户的email id
需要申请email
权限,在登录URL中使用scope
。
SDK版本5:
$loginUrl = $helper->getLoginUrl($redirectUrl, array('scope' => 'email'));
SDK版本4:
$loginUrl = $helper->getLoginUrl(array('scope' => 'email'));
别忘了请求email字段:
$request = new FacebookRequest($session, 'GET', '/me?fields=email');
注意:如果用户不同意与你的应用程序共享电子邮件,你将不会得到它。
根据Facebook政策,如果用户将电子邮件保持为私有,则没有办法获得,否则您可以使用以下行。
$helper->getLoginUrl($redirectUrl, array('scope' => 'email'));