我正在尝试从facebook登录获取电子邮件地址。
请参阅代码
<?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( $fb_app,$fb_key );
// login helper with redirect_uri
$helper = new FacebookRedirectLoginHelper('http://my-site.com/fbconfig.php' );
try {
$session = $helper->getSessionFromRedirect();
} catch( FacebookRequestException $ex ) {
// When Facebook returns an error
} catch( Exception $ex ) {
// 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' );
$response = $request->execute();
// get response
$graphObject = $response->getGraphObject();
$fbid = $graphObject->getProperty('id'); // To Get Facebook ID
$fbfullname = $graphObject->getProperty('first_name');
$femail = $graphObject->getProperty('email'); // To Get Facebook email ID
/* ---- Session Variables -----*/
} else {
$loginUrl = $helper->getLoginUrl();
header("Location: index.html");
}
?>
一切似乎都很好,除了电子邮件。
我使用$femail = $graphObject->getProperty('email');
行来获取用户的电子邮件地址。
谁能告诉我如何解决这个问题?
您应该在获得登录URL时请求email
权限(scope参数)。用户需要授权您访问他/她的电子邮件地址