I am working on facebook login using graph api v2.2 unfortunately I am not able to retrieve the email address from the code below
<?php
session_start();
include_once 'Facebook/autoload.php';
$fb = new Facebook\Facebook([
'app_id' => 'xxx', // Replace {app-id} with your app id
'app_secret' => 'xxxxxx',
'default_graph_version' => 'v2.2',
]);
$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)) {
if ($helper->getError()) {
header('HTTP/1.0 401 Unauthorized');
echo "Error: " . $helper->getError() . "\n";
echo "Error Code: " . $helper->getErrorCode() . "\n";
echo "Error Reason: " . $helper->getErrorReason() . "\n";
echo "Error Description: " . $helper->getErrorDescription() . "\n";
} else {
header('HTTP/1.0 400 Bad Request');
echo 'Bad request';
}
exit;
}
// Logged in
echo '<h3>Access Token</h3>';
var_dump($accessToken->getValue());
// The OAuth 2.0 client handler helps us manage access tokens
$oAuth2Client = $fb->getOAuth2Client();
// Get the access token metadata from /debug_token
$tokenMetadata = $oAuth2Client->debugToken($accessToken);
echo '<h3>Metadata</h3>';
echo '<pre>';
print_r($tokenMetadata);
echo '</pre>';
// Validation (these will throw FacebookSDKException's when they fail)
$tokenMetadata->validateAppId('{app-id}'); // Replace {app-id} with your app id
// If you know the user ID this access token belongs to, you can validate it here
//$tokenMetadata->validateUserId('123');
$tokenMetadata->validateExpiration();
if (! $accessToken->isLongLived()) {
// Exchanges a short-lived access token for a long-lived one
try {
$accessToken = $oAuth2Client->getLongLivedAccessToken($accessToken);
} catch (Facebook\Exceptions\FacebookSDKException $e) {
echo "<p>Error getting long-lived access token: " . $helper->getMessage() . "</p>\n\n";
exit;
}
echo '<h3>Long-lived</h3>';
var_dump($accessToken->getValue());
}
$_SESSION['fb_access_token'] = (string) $accessToken;
$response = $fb->get('/me/?fields=email',$_SESSION['fb_access_token']);
print_r($response);
This is the response I am getting -
Access Token
string(163) "xxx"
Metadata
Facebook\Authentication\AccessTokenMetadata Object
(
[metadata:protected] => Array
(
[app_id] => XXX
[type] => USER
[application] => XXX
[expires_at] => DateTime Object
(
[date] => 2018-08-01 11:57:44.000000
[timezone_type] => 3
[timezone] => UTC
)
[is_valid] => 1
[issued_at] => DateTime Object
(
[date] => 2018-06-02 11:57:44.000000
[timezone_type] => 3
[timezone] => UTC
)
[scopes] => Array
(
[0] => email
[1] => public_profile
)
[user_id] => XXX
)
)
But I am not able to get the email address I was trying to look for code examples over here but couldn't find something the can fit
I was trying to use this $response = $fb->get('/me/?fields=email',$_SESSION['fb_access_token']); in order to get the data
You have to ask for this specific permission to the user.
Specify it in your code with this parameter :
$permissions = ['email']; //here you set all the permisions
that you have to send with this line (or equivalent, in your code) :
$loginUrl = $helper->getLoginUrl('https://example.com/fb-callback.php', $permissions);
This Stack Overflow page seems to answer your questions with more details : Facebook login PHP permissions or see the official Facebook doc .
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.