我知道有很多关于这个主题的主题,但没有一个能解决我的问题。
Facebook SDK返回错误:跨站点请求伪造验证失败。 "州" URL和会话中的参数不匹配。
的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: /");
}
我真的不知道如何修复错误。
答案 0 :(得分:0)
尝试使用
session_start();
在使用fb类之前,在两个文件中