2016-11-04 64 views
0

我想跨域fb登錄使用圖形API。跨站請求僞造驗證失敗,需要永久數據參數狀態丟失

enter image description here

我得到錯誤 「的Facebook SDK返回了一個錯誤:跨站請求僞造驗證失敗必需PARAM。 」國家

第1步代碼「 從持久性數據丟失」:

$fb = new \Facebook\Facebook([ 
       'app_id'    => $this->appId, 
       'app_secret'   => $this->appSecret, 
       'default_graph_version' => $this->apiVersion, 
    ]); 

    $permissions = [ 
        'email',       
        'manage_pages', 
        'business_management', 
        'ads_management' 
        ]; 

    $helper = $fb->getRedirectLoginHelper(); 
    $loginUrl = $helper->getLoginUrl($this->redirectUrl.'?back_to='.$customer_website_url, $permissions); 

步驟3代碼

$fb = new \Facebook\Facebook([ 
    'app_id'    => $appId, 
    'app_secret'   => $appSecret, 
    'default_graph_version' => $graph_api_version, 
]); 

$helper = $fb->getRedirectLoginHelper(); 
$accessToken = $helper->getAccessToken(); 

我在圖像中解釋過的一切,所以任何人可能是什麼問題和可能的解決方案?

+1

當創建登錄URL時,會將'state'參數存儲在會話中。因此,從Facebook返回到應用程序的重定向必須轉到同一個域,因爲這是會話的地方。 – CBroe

回答

0

首先檢查您的PHP腳本是否使用UTF8或** UTF8編碼,而不使用BOM。有時這會導致問題,也可能由於其他原因而發生,其中一個是SESSION處理。 d

步驟3將在FB中APP共享其中的Facebook將採取Fb的應用程序的權限後,將用戶重定向回調PHP腳本:當我回來,在瀏覽器的前進按鈕我得到這個錯誤。如果不同或域不同,你可能會得到這個。

您也可以手動設置state參數。

試試這個

在你步驟3中的代碼,得到訪問令牌後,將其轉換爲長期令牌,設置訪問令牌在會議併爲您GET參數,你」再基於該傳球重定向到頁面

$fb = new \Facebook\Facebook([ 
    'app_id'    => $appId, 
    'app_secret'   => $appSecret, 
    'default_graph_version' => $graph_api_version, 
]); 

$helper = $fb->getRedirectLoginHelper(); 
$accessToken = $helper->getAccessToken(); 
$oAuth2Client = $fb->getOAuth2Client(); 

if (!$accessToken->isLongLived()) { 
    // Exchanges a short-lived access token for a long-lived one 
    try { 
     $accessToken = $oAuth2Client->getLongLivedAccessToken($accessToken); 
    } catch (Facebook\Exceptions\FacebookSDKException $e) { 
     echo "<p>Error getting long-lived access token: " . $helper->getMessage() . "</p>\n\n"; 
     exit; 
    } 
} 

// You can also store this access token in database for future use 
$_SESSION['fb_access_token'] = (string) $accessToken; 

if (isset($_GET['back_to']) and $_GET['back_to'] == '<any_specific_value>') { 
    header("Location: <specific page>"); 
    exit(); 
} else { 
    // User is logged in with a long-lived access token. 
    // You can redirect them to a members-only page. 
    header("Location: <default page>"); 
    exit(); 
} 

<specific page>/<default page>,使用fb_access_token從會話可以查詢Facebook的圖形/營銷阿比

一旦做到這一點,如果你存儲訪問令牌在數據庫中,步驟1碼,你可以檢查你是否會已經有特定用戶的訪問令牌

// Query database for Access token of the user 

if (exists) { 
    // Fetch access Token and use it 
} else { 
    $fb = new \Facebook\Facebook([ 
       'app_id'    => $this->appId, 
       'app_secret'   => $this->appSecret, 
       'default_graph_version' => $this->apiVersion, 
    ]); 

    $permissions = [ 
     'email',       
     'manage_pages', 
     'business_management', 
     'ads_management' 
    ]; 

    $helper = $fb->getRedirectLoginHelper(); 
    $loginUrl = $helper->getLoginUrl($this->redirectUrl.'?back_to='.$customer_website_url, $permissions); 
} 
+0

真正的問題是由於兩個不同的域,我無法在我的域上獲得訪問令牌,因爲請求是從客戶的域發送的。 – Jass

+0

任何關於http://stackoverflow.com/questions/40483537/how-to-add-user-to-facebook-custom-audience的想法? – Jass