Facebook Email field return null (even if the “email” permission is set and accepted) part 2

心不动则不痛 提交于 2020-01-14 07:49:09

问题


Here's a link to the topic which described my initial problem.

In short: the problem is that in some cases facebook-graph-api doesn't return the email address of the user.

Other stackoverflow mates suggested to use his Facebook email if he has a user name (i.e. userName@facebook.com) which I've done. But what to do if the facebook user doesn't have "username" there too.

What would you recommend? To redirect him on the page asking his email address?


回答1:


There are several reasons why Facebook API doesn't return the email address. Some reasons are listed in https://developers.facebook.com/bugs/298946933534016

Different solutions were proposed to handle this case like in a discussion at https://github.com/mkdynamic/omniauth-facebook/issues/61

But I think using userName@facebook.com is not a good solution, because it's not a valid email, and userName might be not available either, and when you will try sending an email to the user, it will fail to deliver, etc.

Asking a user's email address also is not a good thing to do, since some users might refuse to provide their emails.

I recommend constructing such a fake mail like uid@facebook.yourdomain.com where uid is a user ID guaranteed always to return from Facebook, and a subdomain of your domain where you can handle such fake emails like you want.




回答2:


if you are using PHP Sdk then this part is important:

// $facebook->api('/me?fields=id,email,first_name,last_name,gender,birthday');

$facebook = new Facebook\Facebook([
  'app_id' => '{app-id}',
  'app_secret' => '{app-secret}'
  ]);

$accessToken = !empty($_GET['accessToken'])?$_GET['accessToken']:'';
$facebook->setAccessToken($accessToken);
$user = $facebook->getUser();

if ($user) {
  try {
    // Proceed knowing you have a logged in user who's authenticated.
    $user_profile = $facebook->api('/me?fields=id,email,first_name,last_name,gender,birthday');
     // your register function here
     // userSignup($user_profile);
  } catch (FacebookApiException $e) {
    //echo '<pre>'.htmlspecialchars(print_r($e, true)).'</pre>';
    $user = null;
  }
}


来源:https://stackoverflow.com/questions/25069816/facebook-email-field-return-null-even-if-the-email-permission-is-set-and-acce

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