Facebook Graph API not returning email

facebook, facebook-graph-api, facebook-php-sdk

Solution

Add the fields you need to the URL of your request:

https://graph.facebook.com/me?fields=email,name

Problem

I have the following code: ``` $fb = new Facebook([ 'app_id' => $appId, 'app_secret' => $appSecret, 'default_graph_version' => 'v2.9', ]); $oAuth2Client = $fb->getOAuth2Client(); $tokenMetaData = $oAuth2Client->debugToken($accessToken); dump($tokenMetaData); $graphUser = $fb->get('/me?fields=first_name,last_name,email', $accessToken)->getGraphUser()->asArray(); dump($graphUser); ``` The output for the above is the following: $metaData: ``` [ "app_id" => "..." "application" => "My App Name" "expires_at" => "2017-07-01 11:40:09.000000" "is_valid" => true "issued_at" => "2017-05-02 11:40:09.000000" "metadata" => array:2 [ "auth_type" => "rerequest" "sso" => "ios" ] "scopes" => array:2 [ 0 => "email" 1 => "public_profile" ] "user_id" => "102..." ] } ``` $graphUser: ``` array:3 [ "first_name" => "John" "last_name" => "Smith" "id" => "102...", ] ``` As you can see, the `scopes` in `$metaData` clearly has `email` so it isn't a permission issue. Despite this, the graph user sometimes does not have the email (although in some cases it does). Why is this and how can I solve this issue?

Original source