Facebook Graph API not returning email
Asked Answered
S

7

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?

Sane answered 2/5, 2017 at 12:17 Comment(3)
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.Tingaling
@Tingaling please write your comment as an answer so i can accept itSane
@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 somebodyComplaisance
S
20

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

https://graph.facebook.com/me?fields=email,name
Soche answered 30/10, 2017 at 14:9 Comment(1)
That's the right answerAntilepton
M
7

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

Militarize answered 25/3, 2019 at 15:20 Comment(0)
B
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
Billingsgate answered 11/9, 2017 at 4:0 Comment(0)
P
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
    });
    
Pervious answered 29/8, 2018 at 5:13 Comment(0)
N
4

Goto your app > App review > Permissions and Features

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

Nevermore answered 9/2, 2022 at 15:11 Comment(0)
C
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.

Catawba answered 31/5, 2017 at 1:46 Comment(3)
I specified the version, so that does not seem to be the issueSane
I also updated to the latest version but still it fails to return the email :/Farcical
I only get back name,id fields - not emailAntilepton
L
0

From https://developers.facebook.com/tools/explorer after selecting Permissions > Add a Permission > email you can access the Email address through the AccessToken you created by saying Generate Access Token.

var fbUrl = $"me?access_token={accessToken}&fields=name,email"; 

For test https://graph.facebook.com/me?'YourAccessToken'=name,email

Lankford answered 22/12, 2023 at 15:27 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.