正在从Facebook GraphObject检索电子邮件


Retrieving email from Facebook GraphObject

我无法从Graph对象检索电子邮件。我在我的应用程序上看到我有权限使用它。这是我当前的代码:

    FacebookSession::setDefaultApplication('xxx','xx');
    $helper = new FacebookRedirectLoginHelper('xxx');
    $session = null;
    try {
        $session = $helper->getSessionFromRedirect();
        Core::session('FacebookAuthSession')->sessionObject = $session;
    } catch(FacebookRequestException $ex) {
    } catch('Exception $ex) {
    }
    $request = new FacebookRequest($session, 'GET', '/me');
    $response = $request->execute();
    $graphObject = $response->getGraphObject(); 

您可以使用以下代码执行:

use Facebook'FacebookRequest;
use Facebook'GraphUser;
use Facebook'FacebookRequestException;
if($session) {
  try {
    $user_profile = (new FacebookRequest(
      $session, 'GET', '/me'
    ))->execute()->getGraphObject(GraphUser::className());
    echo "Email: " . $user_profile->getEmail();
  } catch(FacebookRequestException $e) {
    echo "Exception occured, code: " . $e->getCode();
    echo " with message: " . $e->getMessage();
  }   
}

要测试结果,还可以使用资源管理器:https://developers.facebook.com/tools/explorer

在您的应用程序设置中,权限仅列为"未经审查即批准",但您仍必须在登录过程中使用"scope"参数向用户授权:https://developers.facebook.com/docs/reference/php/facebook-getLoginUrl/