gpt4 book ai didi

php - Facebook PHP SDK 5::API 2.4::跨站请求伪造验证失败。缺少必需的参数 "state"

转载 作者:行者123 更新时间:2023-11-30 05:16:43 28 4
gpt4 key购买 nike

我做了一个非常简单的 PHP 脚本,只是为了尝试通过 Facebook 登录并获取 accessToken。但是当我尝试以下代码时,我从 SDK 中得到一个异常:« 跨站点请求伪造验证失败。缺少必需的参数“state”。 ».

这是我的代码:

require_once __DIR__ . '/facebook-sdk-v5/autoload.php';
session_start();

$fb = new Facebook\Facebook([
'app_id' => '{my-own-app-id}',
'app_secret' => '{my-own-app-secret}'
]);

// Check to see if we already have an accessToken ?
if (isset($_SESSION['facebook_access_token'] )) {
$accessToken = $_SESSION['facebook_access_token'];
echo "Horray we have our accessToken:$accessToken<br />\n";

} else {
// We don't have the accessToken
// But are we in the process of getting it ?
if (isset($_REQUEST['code'])) {
$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;

// Now you can redirect to another page and use the
// access token from $_SESSION['facebook_access_token']

echo "Finally logged in! Token:$accessToken";
}
} else {
// Well looks like we are a fresh dude, login to Facebook!
$helper = $fb->getRedirectLoginHelper();
$permissions = ['email', 'user_likes']; // optional
$loginUrl = $helper->getLoginUrl('http://mywebsite.com/myapp/index.php', $permissions);

echo '<a href="' . $loginUrl . '">Log in with Facebook!</a>';
}

}

exit;

最佳答案

我不得不在一些服务器中添加这一行:

$helper = $fb->getRedirectLoginHelper();
if (isset($_GET['state'])) {
$helper->getPersistentDataHandler()->set('state', $_GET['state']);
}

我随机收到此错误,具体取决于服务器配置。

关于php - Facebook PHP SDK 5::API 2.4::跨站请求伪造验证失败。缺少必需的参数 "state",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31520593/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com