2017-07-08 54 views
0

我知道這個話題有很多話題,但他們都不會解決我的問題。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: /"); 
} 

我真的不知道如何解決這個錯誤。

+0

難道你包括你的login.php不止一次的過程中,或者說您呼叫的getLoginUrl方法比以前多了一旦? (它會在每次執行的會話中創建一個新的狀態值。) – CBroe

回答

0

嘗試使用

session_start(); 
在這兩個文件

使用FB類

+0

請嘗試解釋爲什麼這可能有助於OP,並可能提供備份您的聲明的參考。 – idmean

+0

這不提供問題的答案。一旦你有足夠的[聲譽](https://stackoverflow.com/help/whats-reputation),你將可以[對任何帖子發表評論](https://stackoverflow.com/help/privileges/comment);相反,[提供不需要提問者澄清的答案](https://meta.stackexchange.com/questions/214173/why-do-i-need-50-reputation-to-comment-what-c​​an- I-DO-代替)。 - [來自評論](/ review/low-quality-posts/17855109) – Hidde

相關問題