Darkh Darkh - 4 months ago 20
PHP Question

Facebook connect permissions PHP SDK

I try to establish a connection to Facebook Connect, the treatment goes well, I can retrieve the user name and id, but the information transmitted in $ permissions are not taken into account. Would I have forgotten a thing?
I followed the docs available on Facebook developers.
I use the latest version: PHP SDK 5.2

public function facebook() {
session_start();
require '../Vendor/autoload.php';
$fb = new Facebook\Facebook([
'app_id' => 'ID_APP',
'app_secret' => 'SECRET_APP',
'default_graph_version' => 'v2.5',
]);
$helper = $fb->getRedirectLoginHelper();
$permissions = ['email', 'user_location','user_birthday']; // optional
$loginUrl = $helper->getLoginUrl('http://localhost/Weade/connexion/check_facebook', $permissions);

$this->redirect($loginUrl);


}


 

public function check_facebook() {
session_start();
require '../Vendor/autoload.php';
$fb = new Facebook\Facebook([
'app_id' => 'APP_ID',
'app_secret' => 'APP_SECRET',
'default_graph_version' => 'v2.5',
]);
$helper = $fb->getRedirectLoginHelper();
try {
$accessToken = $helper->getAccessToken();
} catch(Facebook\Exceptions\FacebookResponseException $e) {
// When Graph returns an error
echo 'Graph returned an error: ' . $e->getMessage();
exit;
} catch(Facebook\Exceptions\FacebookSDKException $e) {
// When validation fails or other local issues
echo 'Facebook SDK returned an error: ' . $e->getMessage();
exit;
}

if (isset($accessToken)) {
// Logged in!
$_SESSION['facebook_access_token'] = (string) $accessToken;

// Now you can redirect to another page and use the
// access token from $_SESSION['facebook_access_token']
}

if($this->Session->read('facebook_access_token')) {
$fb->setDefaultAccessToken($this->Session->read('facebook_access_token'));

try {
$response = $fb->get('/me');
$userNode = $response->getGraphUser();
debug($userNode);


} catch(Facebook\Exceptions\FacebookResponseException $e) {
// When Graph returns an error
echo 'Graph returned an error: ' . $e->getMessage();
exit;
} catch(Facebook\Exceptions\FacebookSDKException $e) {
// When validation fails or other local issues
echo 'Facebook SDK returned an error: ' . $e->getMessage();
exit;
}

}

}


Thanks,

Answer
$response = $fb->get('/me?fields=emai,user_location,user_birthday');

You'll have to define which fields you want to retrieve with your request, for example /me?fields=id,name,email,birthday instead of just /me

See https://developers.facebook.com/docs/apps/changelog#v2_4

To try to improve performance on mobile networks, Nodes and Edges in v2.4 requires that you explicitly request the field(s) you need for your GET requests. For example, GET /v2.4/me/feed no longer includes likes and comments by default, but GET /v2.4/me/feed?fields=comments,likes will return the data. For more details see the docs on how to request specific fields.