错误是什么意思: Facebook SDK返回错误:未从图表中返回访问令牌。 ?
旧代码突然停止,我必须实现一个新的Facebook身份验证脚本。
我做错了什么?我还需要什么?
我的代码:
if(!session_id()) {
session_start();
}
include_once APP_PATH . "plugins/fbsdk-v5/autoload.php";
use Facebook\Facebook;
use Facebook\Exceptions\FacebookResponseException;
use Facebook\Exceptions\FacebookSDKException;
$fb = new Facebook([
'app_id' => $_SESSION['config']['social_facebook_id'],
'app_secret' => $_SESSION['config']['social_facebook_auth'],
'default_graph_version' => 'v2.2',
]);
$helper = $fb->getRedirectLoginHelper();
$permissions = array('email','public_profile');
try {
if(isset($_SESSION['facebook_access_token'])) {
$accessToken = $_SESSION['facebook_access_token'];
}
else {
$accessToken = $helper->getAccessToken();
}
} catch(FacebookResponseException $e) {
echo 'Graph returned an error: ' . $e->getMessage();
} catch(FacebookSDKException $e) {
echo 'Facebook SDK returned an error: ' . $e->getMessage();
}
if(isset($accessToken)){
if(isset($_SESSION['facebook_access_token'])) {
$fb->setDefaultAccessToken($_SESSION['facebook_access_token']);
}
else {
$_SESSION['facebook_access_token'] = strval($accessToken);
$oAuth2Client = $fb->getOAuth2Client();
$longLivedAccessToken = $oAuth2Client->getLongLivedAccessToken($_SESSION['facebook_access_token']);
$_SESSION['facebook_access_token'] = strval($longLivedAccessToken);
$fb->setDefaultAccessToken($_SESSION['facebook_access_token']);
}
if(isset($_GET['code'])){
header('Location: ./');
}
try {
$profileRequest = $fb->get('/me?fields=id,name,email', $accessToken);
$fbUserProfile = $profileRequest->getGraphNode()->asArray();
} catch(FacebookResponseException $e) {
echo 'Graph returned an error: ' . $e->getMessage();
session_destroy();
header("Location: ./");
exit;
} catch(FacebookSDKException $e) {
echo 'Facebook SDK returned an error: ' . $e->getMessage();
exit;
}
$fbUserData = array(
'oauth_provider'=> 'facebook',
'oauth_uid' => $fbUserProfile['id'],
'first_name' => $fbUserProfile['first_name'],
'last_name' => $fbUserProfile['last_name'],
'email' => $fbUserProfile['email'],
'gender' => $fbUserProfile['gender'],
'locale' => $fbUserProfile['locale'],
'picture' => $fbUserProfile['picture']['url'],
'link' => $fbUserProfile['link']
);
$_SESSION['userData'] = $userData;
$logoutUrl = $helper->getLogoutUrl($accessToken, APP_URL . "users/logout.html");
}
else {
$loginUrl = $helper->getLoginUrl(APP_URL . "users/login-using-facebook.html", $permissions);
}
我的新代码:
<?php
include_once APP_PATH . "plugins/fbsdk-v5/autoload.php";
use Facebook\Facebook;
use Facebook\Exceptions\FacebookResponseException;
use Facebook\Exceptions\FacebookSDKException;
$fb = new Facebook(array(
'app_id' => $_SESSION['config']['social_facebook_id'],
'app_secret' => $_SESSION['config']['social_facebook_auth'],
'default_graph_version' => 'v2.3',
));
if($_GET['action'] == 'login-using-facebook') { // callback
$helper = $fb->getRedirectLoginHelper();
try {
$accessToken = $helper->getAccessToken();
var_dump($accessToken);
} 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
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);
var_dump($tokenMetadata);
// Validation (these will throw FacebookSDKException's when they fail)
$tokenMetadata->validateAppId($_SESSION['config']['social_facebook_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;
}
var_dump($accessToken->getValue());
}
$_SESSION['fb_access_token'] = strval($accessToken);
}
elseif($_SESSION['fb_access_token']) { // retrieve
try {
// Returns a `Facebook\FacebookResponse` object
$response = $fb->get('/me?fields=id,name', $_SESSION['fb_access_token']);
} catch(Facebook\Exceptions\FacebookResponseException $e) {
echo 'Graph returned an error: ' . $e->getMessage();
exit;
} catch(Facebook\Exceptions\FacebookSDKException $e) {
echo 'Facebook SDK returned an error: ' . $e->getMessage();
exit;
}
$user = $response->getGraphUser();
echo 'Name: ' . $user['name'];
}
else { // login
$helper = $fb->getRedirectLoginHelper();
$permissions = array('email');
$loginUrl = $helper->getLoginUrl(APP_URL."users/login-using-facebook.html", $permissions);
}
?>
我使用了developers.facebook.com/docs/php/howto/example_facebook_logi n和developers.facebook.com/docs/php/howto/profilewithgraphapi但它仍然不起作用:(我无法理解为什么示例代码在我的脚本中是错误的。
有人能帮助我吗?