我知道這個話題有很多話題,但他們都不會解決我的問題。Facebook SDK返回錯誤:跨站請求僞造驗證失敗。來自URL和會話的「狀態」參數不匹配?
Facebook SDK returned an error: Cross-site request forgery validation failed. The "state" param from the URL and session do not match.
的login.php:
require_once '/../../../vendor/autoload.php';
$fb = new Facebook\Facebook([
'app_id' => 'appid',
'app_secret' => 'appsecret',
'default_graph_version' => 'v2.8',
]);
$helper = $fb->getRedirectLoginHelper();
$permissions = ['email', 'public_profile', 'user_birthday', 'user_friends', 'user_location']; // optional
$loginUrl = $helper->getLoginUrl('https://website.com/login-callback.php', $permissions);
登錄-callback.php:
include("library/config.php");
include $_SERVER['DOCUMENT_ROOT']. '/../../../vendor/autoload.php';
$fb = new Facebook\Facebook([
'app_id' => 'appid',
'app_secret' => 'appsecret',
'default_graph_version' => 'v2.8',
]);
$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;
try {
// Returns a `Facebook\FacebookResponse` object
$response = $fb->get('/me?fields=id,name,first_name,last_name,birthday,email,link,gender,locale,verified,friends,location', $accessToken);
} 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;
}
//echo $response->getGraphUser();
$user = $response->getGraphUser();
$_SESSION['facebook_uid'] = $user->getId();
/* Do Login Things -> Database update etc. */
header("Location: /");
}
我真的不知道如何解決這個錯誤。
難道你包括你的login.php不止一次的過程中,或者說您呼叫的getLoginUrl方法比以前多了一旦? (它會在每次執行的會話中創建一個新的狀態值。) – CBroe