Facebook Graph API not returning email

北城以北 提交于 2020-08-18 06:25:06

问题


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?


回答1:


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

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



回答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



回答3:


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




回答4:


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.




回答5:


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
    });
    


来源:https://stackoverflow.com/questions/43737695/facebook-graph-api-not-returning-email

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!