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. [email protected]) 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?
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;
}
}
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 [email protected]
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 [email protected]
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With