Failed to get access token from facebook?

login.php:

require_once "/vendor/autoload.php";

  $fb = new Facebook\Facebook([
    "app_id" => "xxx",
    "app_secret" => "xxx", 
    "default_graph_version" => "v2.8",
    ]);

  $helper = $fb->getRedirectLoginHelper();

  $permissions = ["email"];
  $loginUrl = $helper->getLoginUrl("https://xxx.xxx.com/fb-sdk/fb-callback.php", $permissions);

  echo "<a href="" . htmlspecialchars($loginUrl) . "">Log in with Facebook!</a>";

fb-callback.php:

require_once "/vendor/autoload.php";

$fb = new \Facebook\Facebook([
  "app_id" => "xxx",
  "app_secret" => "xxx",
  "default_graph_version" => "v2.8",

]);

// Use one of the helper classes to get a Facebook\Authentication\AccessToken entity.
$helper = $fb->getRedirectLoginHelper();
//   $helper = $fb->getJavaScriptHelper();
//   $helper = $fb->getCanvasHelper();
//   $helper = $fb->getPageTabHelper();

try {
  $accessToken = $helper->getAccessToken();
  // Get the \Facebook\GraphNodes\GraphUser object for the current user.
  // If you provided a "default_access_token", the "{access-token}" is optional.
  $response = $fb->get("/me?fields=id,name,email", $accessToken);
} 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;
}

$me = $response->getGraphUser();
echo "Logged in as " . $me->getName();

he will get stuck in $accessToken = $helper- > getAccessToken (); and will not run.
print out any news
and report:
Facebook SDK returned an error: Cross-site request forgery validation failed.. Required param "state" missing from persistent data.

he replied to my fb-callback.php with these two key

fb-callback.php?code=***&state=***-sharp_=_

I think it"s token, so I try to replace $accessToken
, but it"s still wrong to say that token format is not right for
. What"s wrong with it?
I"m sure login is OK.
when I enter login.php and press Login, I will go to facebook, and ask if I agree. I press yes and go straight to fb-callback.php, but there is no token in my report.

I follow this: https://github.com/facebook/p.

Mar.14,2021

here's the thing:

  1. first you need to use graph_versionv2.12 or v3.0. V2.8 has been eliminated and an error will be reported later.
  2. it is very possible that there is a problem with your server settings, so you cannot pass state to sdk. Solution:
// fb-callback.phpsession
if (!session_id()) { 
    session_start(); 
}

// 
$helper = $fb->getRedirectLoginHelper(); 
if (isset($_GET['state'])) { 
    $helper->getPersistentDataHandler()->set('state', $_GET['state']); 
}

talk about the oauth2.0 authorization process, which is applicable to all oauth2.0 applications, such as twitter,google,facebook,etc.

  1. construct an authorization link, that is, $loginUrl = $helper- > getLoginUrl ('https://xxx.xxx.com/fb-sdk/fb-callback.php', $permissions) . Generally, you need to pass in a state (usually a random string)
  2. after authorization is completed, a third party (in this case, facebook) passes state and code parameters to callback. State needs to match the first step. If it does not match, it is proved that it is not replied by a third party.
  3. use code for access_token and Openid
  4. use access_token and openid access to get user information interface

at this point, the authorization process ends

Menu