简体   繁体   中英

How to retrieve email from facebook user profile with SDK 4.4.0 and API 2.3?

I've a small issue related to the retrievement of the email of a user with Facebook SDK 4.4.0 / API 2.3 .

I created my application through the developer facebook area and actually my goal in my PHP scripts is to login into Facebook through my facebook application to retrieve some information of the logged user to create in my website a basic profile. I've two PHP scripts, the first gets the login url for facebook, the second retrieves user data after authentication.

login.php

require_once(dirname(__FILE__).'/libs/facebook-php-sdk-v4-4.0-dev/autoload.php');

Facebook\FacebookSession::setDefaultApplication($a_config['settings']['facebook_app_id'], $a_config['settings']['facebook_app_secret']);

$o_helper = new Facebook\FacebookRedirectLoginHelper($s_facebook_login_backurl);
$s_facebook_login_url = $o_helper->getLoginUrl();

Through this $s_facebook_login_url I login in the facebook application, authenticate myself as a user and then come back to my second script through the parameter $s_facebook_login_backurl . Here I have my commands to get data from my profile. Everything I need works fine, except for the email, this element is not found.

registration.php

require_once(dirname(__FILE__).'/libs/facebook-php-sdk-v4-4.0-dev/autoload.php');

Facebook\FacebookSession::setDefaultApplication($a_config['settings']['facebook_app_id'], $a_config['settings']['facebook_app_secret']);


$o_social = Facebook\FacebookSession::newAppSession();
try {
      $o_social->validate();
}
catch(...){
  // exceptions
}

or

$o_helper = new Facebook\FacebookRedirectLoginHelper($s_facebook_login_backurl);
$o_social = $o_helper->getSessionFromRedirect();

then

$s_request = '/me?fields=id,name,email,last_name,first_name,middle_name';
$o_request = new Facebook\FacebookRequest($o_social, 'GET', $s_request);
$o_response = $o_request->execute();

$o_graph_object = $o_response->getGraphObject();
$a_media = $o_graph_object->asArray();
$s_facebook_id = $a_media['id'];
$s_facebook_name = $a_media['first_name'];
$s_facebook_inbetweenname = $a_media['middle_name'];
$s_facebook_surname = $a_media['last_name'];
$s_facebook_email = $a_media['email'];

The issue is that the last variable $s_facebook_email is totally empty.

In the developer area from facebook, in the Graph API Explorer I even test my GET request to my facebook application, and the email is retrieved, I set the permissions of the user (in User data permissions) and of the email (in Extended permissions) but through the SDK the email is the only element missing.

在此处输入图片说明

I tried even to erase the application from my user settings, and give the authentication again, but it did not work. Has anybody some ideas to get the email? I find online pretty much always the same suggestions, but probably I miss something.

Thanks in advance

As per the docs , you can specify what permissions you would like to ask from the user:

# login.php
$fb = new Facebook\Facebook([/* . . . */]);

$helper = $fb->getRedirectLoginHelper();
$permissions = ['email', 'user_likes']; // optional
$loginUrl = $helper->getLoginUrl('http://{your-website}/login-callback.php', $permissions);

echo '<a href="' . $loginUrl . '">Log in with Facebook!</a>';

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM