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
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.
First check if your access token gets the user’s permission for the email
if in the answer, this content does not appear:
Maybe in obtaining your access token I do not request mail ownership: Add scope=email to the request
Add the fields you need to the URL of your request:
You have to check these steps.
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.
check the facebook version, use the latest version.
add email scope in face book login script as below
or
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 usingmobile number
tologin
, and even in my profileprimary email
is not set or set to amobile number
, in this casefacebook
returns null foremail
.I have updated the account and added email to
primary email
field undersettings
infacebook page
, then i was able to get theemail id
fromfacebook
.Hope this helps someone.
Happy coding…
Goto your app > App review > Permissions and Features
Request Advanced access for email which should turn Standard Access into Advanced Access