can't get email address from Facebook SDK v5 for PHP -
i want user's email address after logging facebook using php sdk v5, running issues don't quite understand.
according step step guide https://developers.facebook.com/docs/reference/php/5.0.0
i create login.php , login-callback.php files
login.php
session_start() ; require_once __dir__ . '/src/facebook/autoload.php'; $fb = new facebook\facebook([ 'app_id' => $app_id, 'app_secret' => $app_secret, 'default_graph_version' => 'v2.2', ]); $helper = $fb->getredirectloginhelper(); $permissions = ['email', 'public_profile']; // optional $loginurl = $helper->getloginurl('http://www.example.com/login-callback.php', $permissions); echo '<a href="' . $loginurl . '">log in facebook!</a>';
login-callback.php
session_start() ; require_once __dir__ . '/src/facebook/autoload.php'; # login-callback.php $fb = new facebook\facebook([ 'app_id' => $app_id, 'app_secret' => $app_secret, 'default_graph_version' => 'v2.2', ]); $helper = $fb->getredirectloginhelper(); try { $accesstoken = $helper->getaccesstoken(); } catch(facebook\exceptions\facebookresponseexception $e) { // when graph returns error echo 'graph returned error: ' . $e->getmessage(); exit; } catch(facebook\exceptions\facebooksdkexception $e) { // when validation fails or other local issues echo 'facebook sdk returned error: ' . $e->getmessage(); exit; } if (isset($accesstoken)) { // logged in! $_session['facebook_access_token'] = (string) $accesstoken; // oauth 2.0 client handler $oauth2client = $fb->getoauth2client(); // exchanges short-lived access token long-lived 1 $longlivedaccesstoken = $oauth2client->getlonglivedaccesstoken($_session['facebook_access_token']); // sets default fallback access token don't have pass each request $fb->setdefaultaccesstoken($longlivedaccesstoken); try { $response = $fb->get('/me'); $usernode = $response->getgraphuser(); } catch(facebook\exceptions\facebookresponseexception $e) { // when graph returns error echo 'graph returned error: ' . $e->getmessage(); exit; } catch(facebook\exceptions\facebooksdkexception $e) { // when validation fails or other local issues echo 'facebook sdk returned error: ' . $e->getmessage(); exit; } echo 'logged in ' . $usernode->getname(); $graphobject = $response->getgraphobject(); $email = $graphobject->getproperty('email'); // not getting thing echo $email; // empty }
the graphobject->getproperty('email'); not returning anything
my problem same. site recognized login, received name , id.
i saw hint here:
$fb->get('/me?fields=email');
this fixed problem!
Comments
Post a Comment