跨站点请求伪造验证失败,要求持久数据中缺少参数状态

时间:2016-11-04 10:00:37

标签: php facebook-graph-api facebook-marketing-api

我正在使用图谱API尝试跨域fb登录。

enter image description here

我收到错误“Facebook SDK返回错误:跨站点请求伪造验证失败。持久数据中缺少必需的参数”状态“

第1步代码:

false

第3步代码

$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);

我在Image中解释过的所有内容,以便任何人可能出现问题和可能的解决方案?

1 个答案:

答案 0 :(得分:0)

首先检查您的PHP脚本是否使用 UTF8或** UTF8(无BOM)进行编码。有时这会导致问题,可能由于某些其他原因而导致其中一个原因是 SESSION处理。当我在浏览器中尝试前进和后退按钮时,我收到此错误:D

第3步将在Fb APP中共享回调php脚本,其中 Facebook 将在获取 Fb App权限后重定向用户。如果它不同或域名不同,您可能会得到这个。

您也可以手动设置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 Graph / Marketing Api

完成此操作并且如果您在数据库中存储访问令牌,在第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);
}
相关问题