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

时间:2017-10-13 15:12:32

标签: javascript php facebook api login

Hello Comunity我收到此错误。我已经尝试过两个脚本中的session_start()等解决方案。

现在fb登录工作正常,很少有像我这样的帐户和项目中的其他开发人员。但是,当其他客户试图获得它时,它无法正常工作

这是我的代码。如果有人可以帮助我会很棒。

谢谢和问候

<?php


# Start the session 
    session_start();
# Autoload the required files
require_once __DIR__ . '/vendor/autoload.php';

# Set the default parameters
$fb = new Facebook\Facebook([
  'app_id' => '129284427705486',
  'app_secret' => 'b08b2cb086d211762b7587deafcde564',
  'default_graph_version' => 'v2.5',
]);
$redirect = 'http://hola.com/sub/facebook/index.php';

# Create the login helper object
$helper = $fb->getRedirectLoginHelper();

# Get the access token and catch the exceptions if any
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 the 
if (isset($accessToken)) {
      // Logged in!
     // Now you can redirect to another page and use the
      // access token from $_SESSION['facebook_access_token'] 
      // But we shall we the same page

    // Sets the default fallback access token so 
    // we don't have to pass it to each request
    $fb->setDefaultAccessToken($accessToken);

    try {
      $response = $fb->get('/me?fields=email,name,picture.width(250).height(250)');
      $userNode = $response->getGraphUser();
    }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;
    }

    $completename = $userNode->getName();
    $iduser = $userNode->getId();
    $username = str_replace(' ', '', $completename);
    $emailuser = $userNode->getProperty('email');
    $pictureuser = $userNode->getPicture();
    $picture = $pictureuser['url'];
    $userlocation = $userNode->getLocation();
    $bdconect = new Mysqli();
    $query = "SELECT * FROM users WHERE id = $iduser";
    $array = $bdconect->query($query);


    if($array->num_rows == 0){
        $query = "INSERT INTO users VALUES('".$iduser."','".$username."',NULL,NULL,'".$completename."','".$emailuser."','".$picture."',NULL,NULL,0,NULL,NULL)";
        $insertuserfb = $bdconect->query($query);
        if($insertuserfb->num_rows == 1){
            setSession($username,$iduser);
        }
        else{
             setSession($username,$iduser);
        }

    }
    else{
         setSession($username,$iduser);
    }

}else{
    $permissions  = ['email'];
    $loginUrl = $helper->getLoginUrl($redirect,$permissions);
    header("Location: $loginUrl");
}



 function setSession($completename,$iduser){
    $_SESSION['username'] = $completename;
    $_SESSION['userid'] = $iduser;
    $_SESSION['picture'] = $picture;
            header('Location: webpage');

}

0 个答案:

没有答案