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.