我正在尝试从facebok登录获取电子邮件地址。
请参阅代码
<?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');
来获取用户的电子邮件地址。
有人能指出我如何解决这个问题吗?
答案 0 :(得分:1)
获取登录URL时,您应该询问email
权限(范围参数)。用户需要授予您访问其电子邮件地址的权限。