问题描述
protected function getFacebook()
{
static $facebook = null;
if($facebook == null){
$facebook = new Facebook\Facebook([
'app_id' => $this->getAppId(),'app_secret' => $this->getAppSecret(),'default_graph_version' => 'v2.10'
]);
}
return $facebook;
}
public function getLoginUrl($url)
{
$fb = $this->getFacebook();
$helper = $fb->getRedirectLoginHelper();
$autorisations = ['email'];
return $helper->getLoginUrl($url,$autorisations);
}
public function callback(&$error = null)
{
$fb = $this->getFacebook();
$helper = $fb->getRedirectLoginHelper();
try {
$accesstoken = $helper->getAccesstoken();
} catch(Facebook\Exception\ResponseException $e) {
// When Graph returns an error
$error = 'Graph returned an error: ' . $e->getMessage();
return false;
} catch(Facebook\Exception\SDKException $e) {
// When validation fails or other local issues
$error = 'Facebook SDK returned an error: ' . $e->getMessage();
return false;
}
....
}
我愿意
$url = $Facebook->getLoginUrl(URL);
在回调文件中
$token = $Facebook->callback($error);
当我点击链接时,回调文件被执行,$helper->getAccesstoken();导致此错误: 未捕获的 Facebook\Exceptions\FacebookSDKException:跨站点请求伪造验证失败。持久数据中缺少必需的参数“状态”。
我看过有关此的帖子,但没有修复对我有用
编辑: 我目前发现的是:Facebook SDK error: Cross-site request forgery validation failed. Required param "state" missing from persistent data Cross-site request forgery validation failed required param state missing from persistent data 和 https://github.com/facebookarchive/php-graph-sdk/issues/1123 https://github.com/facebookarchive/php-graph-sdk/issues/1134
解决方法
最后,我通过在 config.php 中添加相同站点到 Lax 解决了我的问题
ini_set('session.cookie_samesite','Lax');