19

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?

Yahya Uddin
  • 26,997
  • 35
  • 140
  • 231
  • 9
    The API will only returned confirmed email addresses. And there is a bunch of other factors such as privacy settings that can also influence this. So don’t write your app so that it _relies_ on getting an email address from the API. Lots of users don’t have one on file with Facebook, if they signed up using just their mobile. – CBroe May 02 '17 at 12:30
  • @CBroe please write your comment as an answer so i can accept it – Yahya Uddin Aug 29 '18 at 22:04
  • @YahyaUddin, Also, there are lots of user who actually signed up from their mobile phones & they don't have an email at all on their account. Quite old yet, might help somebody – Kasun Rajapaksha May 21 '21 at 05:29

6 Answers6

20

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

https://graph.facebook.com/me?fields=email,name
Agu Dondo
  • 12,638
  • 7
  • 57
  • 68
6

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...

Abhinaw Kaushik
  • 607
  • 6
  • 18
5

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
Samuel Seda
  • 2,814
  • 1
  • 24
  • 13
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
    });
    
Minu Alex
  • 41
  • 4
3

Goto your app > App review > Permissions and Features

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

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.

Eric Guan
  • 15,474
  • 8
  • 50
  • 61