从Facebook GraphObject检索电子邮件

时间:2014-11-10 19:33:01

标签: php facebook facebook-graph-api facebook-php-sdk facebook-sdk-4.0

我无法从图形对象中检索电子邮件。我在我的应用程序上看到我已获得许可。这是我目前的代码:

    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(); 

2 个答案:

答案 0 :(得分:0)

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

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

答案 1 :(得分:0)

您的应用设置中的权限仅列为“未经审核批准”,但您仍需要使用登录过程中的“scope”参数向用户授权:https://developers.facebook.com/docs/reference/php/facebook-getLoginUrl/