skip to Main Content

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?

6

Answers


  1. I solved this issue by specifying the graph api version.

    My requested fields first_name,last_name,email,picture

    To url: https://graph.facebook.com/me

    Did NOT return email.

    To url: https://graph.facebook.com/v2.9/me

    Did return email.

    Hope this helps.

    Login or Signup to reply.
  2. First check if your access token gets the user’s permission for the email

    https://graph.facebook.com/me/permissions?
      access_token=(access-token)
      &debug=all
    

    if in the answer, this content does not appear:

    {
        "data": [
            {
                "permission": "email",
                "status": "granted"
            },
            {
                "permission": "public_profile",
                "status": "granted"
            }
        ]
    }
    

    Maybe in obtaining your access token I do not request mail ownership: Add scope=email to the request

    https://www.facebook.com/v2.10/dialog/oauth?
      client_id=(app-id)
      &redirect_uri=(redirect-uri)
      &scope=email
    
    Login or Signup to reply.
  3. Add the fields you need to the URL of your request:

    https://graph.facebook.com/me?fields=email,name
    
    Login or Signup to reply.
  4. You have to check these steps.

    1. check if your access token gets the user’s permission for the email . for that login to your developer account of your app, then tools->graph api explorer then enable user’s permission for the email.

    2. check the facebook version, use the latest version.

    3. add email scope in face book login script as below

      app.get(
          '/auth/facebook',
          passport.authenticate('facebook', {
            scope: ['user_location', 'email', 'user_friends']
          })
        );
      

      or

      FB.login(function(response) { 
      
         // your ajax script
      
      },{
          scope: 'email', 
          return_scopes: true
      });
      
    Login or Signup to reply.
  5. One possibility which i came across today is, if i am registering a account where i am not logging in using my email ID, may be using mobile number to login, and even in my profile primary email is not set or set to a mobile number, in this case facebook returns null for email.

    I have updated the account and added email to primary email field under settings in facebook page, then i was able to get the email id from facebook.

    Hope this helps someone.

    Happy coding…

    Login or Signup to reply.
  6. Goto your app > App review > Permissions and Features

    Request Advanced access for email which should turn Standard Access into Advanced Access

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search