Facebook SDK返回错误:跨站点请求伪造验证失败。必需的参数“状态”缺失

时间:2017-09-21 15:10:00

标签: php facebook facebook-graph-api

请我尝试整合facebook sdk进行登录,我已经使用xampp在我的系统上成功实现了没有问题, 但是,当我迁移到服务器时,我开始出现此错误

Facebook SDK返回错误:跨站点请求伪造验证失败。必需的参数“状态”缺失。

我在网上查看了其他有问题的人,他们中的大多数都解决了他们在我们尝试的代码开始时打开会话但我仍然有错误

请帮忙

这是我的代码我不知道还有什么要做的,我第一次遇到这样的

<?php
session_start();?>
<?php $title = 'Vote Now'; ?>

 <?php


error_reporting(0);
// Include FB config file && User class
require_once 'fbConfig.php';
require_once 'User.php';

if(isset($accessToken)){
    if(isset($_SESSION['facebook_access_token'])){
        $fb->setDefaultAccessToken($_SESSION['facebook_access_token']);
    }else{
        // Put short-lived access token in session
        $_SESSION['facebook_access_token'] = (string) $accessToken;

        // OAuth 2.0 client handler helps to manage access tokens
        $oAuth2Client = $fb->getOAuth2Client();

        // Exchanges a short-lived access token for a long-lived one
        $longLivedAccessToken = $oAuth2Client->getLongLivedAccessToken($_SESSION['facebook_access_token']);
        $_SESSION['facebook_access_token'] = (string) $longLivedAccessToken;

        // Set default access token to be used in script
        $fb->setDefaultAccessToken($_SESSION['facebook_access_token']);
    }

    // Redirect the user back to the same page if url has "code" parameter in query string
    if(isset($_GET['code'])){
        header('Location: xxxxxxxxxxxxxxxxx');
    }

    // Getting user facebook profile info
    try {
        $profileRequest = $fb->get('/me?fields=name,first_name,last_name,email,link,gender,locale,picture');
        $fbUserProfile = $profileRequest->getGraphNode()->asArray();
    } catch(FacebookResponseException $e) {
        echo 'Graph returned an error: ' . $e->getMessage();
        session_destroy();
        // Redirect user back to app login page
        header("Location: ./index.php");
        exit;
    } catch(FacebookSDKException $e) {
        echo 'Facebook SDK returned an error: ' . $e->getMessage();
        exit;
    }

    // Initialize User class
    $user = new User();

    // Insert or update user data to the database
    $fbUserData = array(
        'oauth_provider'=> 'facebook',
        'oauth_uid'     => $fbUserProfile['id'],
        'first_name'    => $fbUserProfile['first_name'],
        'last_name'     => $fbUserProfile['last_name'],
        'email'         => $fbUserProfile['email'],
        'gender'        => $fbUserProfile['gender'],
        'locale'        => $fbUserProfile['locale'],
        'picture'       => $fbUserProfile['picture']['url'],
        'link'          => $fbUserProfile['link']
    );
    $userData = $user->checkUser($fbUserData);

    // Put user data into session
    $_SESSION['userData'] = $userData;

    // Get logout url
    $logoutURL = $helper->getLogoutUrl($accessToken, $redirectURL.'logout.php');

    // Render facebook profile data
    if(!empty($userData)){
        $output  = '<h1>Facebook Profile Details </h1>';
        $output .= '<img src="'.$userData['picture'].'">';
       $output .= '<br/>Facebook ID : ' . $userData['oauth_uid'];
       $output .= '<br/>Name : ' . $userData['first_name'].' '.$userData['last_name'];
      $output .= '<br/>Email : ' . $userData['email'];
       //$output .= '<br/>Gender : ' . $userData['gender'];
       //$output .= '<br/>Locale : ' . $userData['locale'];
       //$output .= '<br/>Logged in with : Facebook';
        //$output .= '<br/><a href="'.$userData['link'].'" target="_blank">Click to Visit Facebook Page</a>';
       // $output .= '<br/>Logout from <a href="logout.php">Facebook</a>'; 
    }else{
        $output = '<h3 style="color:red">Some problem occurred, please try again.</h3>';
    }

}else{
    // Get login url
    $loginURL = $helper->getLoginUrl($redirectURL, $fbPermissions);

    // Render facebook login button
    //$output = '<meta http-equiv="refresh" content="2; url='.htmlspecialchars($loginURL).'" />';
    $output = '<div align="center"><a style="font-size:21px" href='.htmlspecialchars($loginURL).' />Login On Facebook To Vote</a></div>';
}
?>

<?php
if(!session_id()){
    session_start();
}

// Include the autoloader provided in the SDK
require_once __DIR__ . '/facebook-php-sdk/autoload.php';

// Include required libraries
use Facebook\Facebook;
use Facebook\Exceptions\FacebookResponseException;
use Facebook\Exceptions\FacebookSDKException;

/*
 * Configuration and setup Facebook SDK
 */
$appId          = 'xxxxxxxxxxxxxxxxx'; //Facebook App ID
$appSecret      = 'xxxxxxxxxxxxxxxxx'; //Facebook App Secret
$redirectURL    = 'xxxxxxxxxxxxxxxxx'; //Callback URL
$fbPermissions  = array('email');  //Optional permissions

$fb = new Facebook(array(
    'app_id' => $appId,
    'app_secret' => $appSecret,
    'default_graph_version' => 'v2.2',
));

// Get redirect login helper
$helper = $fb->getRedirectLoginHelper();

// Try to get access token
try {
    if(isset($_SESSION['facebook_access_token'])){
        $accessToken = $_SESSION['facebook_access_token'];
    }else{
        $accessToken = $helper->getAccessToken();
    }
} catch(FacebookResponseException $e) {
    echo 'Graph returned an error: ' . $e->getMessage();
    exit;
} catch(FacebookSDKException $e) {
    echo 'Facebook SDK returned an error: ' . $e->getMessage();
    exit;
}

?>

1 个答案:

答案 0 :(得分:0)

发现解决方案在位置网址中有一个空格

if(isset($_GET['code'])){
            header('Location: xxxxxxxxxxxxxxxxx');
        } 





if(isset($_GET['code'])){
            header('Location:xxxxxxxxxxxxxxxxx');
        }