Ich versuche ein gespeichertes Long-Life Access Token zu verwenden, aber nach 2 Stunden bekomme ich den untenstehenden Fehler von der Graph API. Ich habe einen Code geschrieben, um den Benutzer an Facebook zu senden, um einen neuen Code zu erhalten, der gegen ein Zugriffstoken ausgetauscht werden kann. Das funktioniert einwandfrei, außer dass dies bei jeder folgenden Seitenanfrage geschieht. Facebook setzt weiterhin mein Zugriffs-Token mit dem folgenden Fehler außer Kraft. obwohl dieses Zugriffstoken von ihrem Server zurückgegeben wird.Facebook PHP/JS SDK "Session ist zur Unix-Zeit abgelaufen" mit Long Life Token
Error validating access token: Session has expired at unix time 1338300000. The current unix time is 1338369365.
Das vollständige Beispiel der Testseite finden Sie weiter unten. Meine Schlüssel aus offensichtlichen Gründen weglassen. Drücken Sie die Seite, loggen Sie sich ein und lassen Sie sie für ein paar Stunden und drücken Sie die Seite erneut (Sie erhalten die Umleitung zu Facebook und zurück mit dem Code in der URL), laden Sie die Seite neu und es wird weiter nach Facebook und zurück umgeleitet , Ereignis, obwohl ich es sage, das Zugangstoken zu benutzen, das es gerade für besagten Code zurückgebracht hat.
<?php
require 'facebook.php';
$app_id = APP_ID;
$app_secret = APP_SERCRET;
$my_url = URL;
$facebook = new Facebook(array(
'appId' => $app_id,
'secret' => $app_secret
));
// known valid access token stored in a database
$access_token = isset($_COOKIE["FB_LONG_AC_TOKEN"]) ? $_COOKIE["FB_LONG_AC_TOKEN"] : false;
$code = $_REQUEST["code"];
// If we get a code, it means that we have re-authed the user
//and can get a valid access_token.
if (isset($code)) {
$token_url="https://graph.facebook.com/oauth/access_token?client_id="
. $app_id . "&redirect_uri=" . urlencode($my_url)
. "&client_secret=" . $app_secret
. "&code=" . $code . "&display=popup";
$response = file_get_contents($token_url);
$params = null;
parse_str($response, $params);
$access_token = $params['access_token'];
}
// Attempt to query the graph:
$graph_url = "https://graph.facebook.com/me?"
. "access_token=" . $access_token;
$response = curl_get_file_contents($graph_url);
$decoded_response = json_decode($response);
//Check for errors
if ($decoded_response->error) {
// check to see if this is an oAuth error:
if ($decoded_response->error->type== "OAuthException") {
// Retrieving a valid access token.
$dialog_url= "https://www.facebook.com/dialog/oauth?"
. "client_id=" . $app_id
. "&redirect_uri=" . urlencode($my_url);
echo("<script> top.location.href='" . $dialog_url
. "'</script>");
}
else {
echo "other error has happened";
}
}
else {
// success
echo("Success: ".$decoded_response->name."<br />");
echo($access_token."<br />");
// Attempt to convert access token to longlife token if we don't have one stored.
if (!isset($_COOKIE["FB_LONG_AC_TOKEN"]))
{ // don't have long life token, so let's get one.
$ch = curl_init("https://graph.facebook.com/oauth/access_token?client_id=".$app_id."&client_secret=".$app_secret."&grant_type=fb_exchange_token&fb_exchange_token=".$access_token);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 5);
curl_setopt($ch, CURLOPT_TIMEOUT, 10);
$data = curl_exec($ch);
curl_close($ch);
$params = null;
parse_str($data, $params);
if (isset($params["access_token"]))
{
$access_token = $params["access_token"];
echo("Got long life token.<br />");
setcookie("FB_LONG_AC_TOKEN", $access_token, time() + (3600 * 24 * 60), "/");
}
}
else {
echo("Have long life token already.<br />");
}
}
if ($access_token) {
$facebook->setAccessToken($access_token);
// See if there is a user from a cookie
$user = $facebook->getUser();
if ($user) {
try {
// Proceed knowing you have a logged in user who's authenticated.
$user_profile = $facebook->api('/me');
} catch (FacebookApiException $e) {
echo '<pre>'.htmlspecialchars(print_r($e, true)).'</pre>';
$user = null;
}
}
}
// note this wrapper function exists in order to circumvent PHP’s
//strict obeying of HTTP error codes. In this case, Facebook
//returns error code 400 which PHP obeys and wipes out
//the response.
function curl_get_file_contents($URL) {
$c = curl_init();
curl_setopt($c, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($c, CURLOPT_URL, $URL);
$contents = curl_exec($c);
$err = curl_getinfo($c,CURLINFO_HTTP_CODE);
curl_close($c);
if ($contents) return $contents;
else return FALSE;
}
?>
<!doctype html>
<html xmlns:fb="http://www.facebook.com/2008/fbml">
<head>
<title>Facebook Auth</title>
</head>
<body>
<?php if ($user) { ?>
Your user profile is
<pre>
<?php print htmlspecialchars(print_r($user_profile, true)) ?>
</pre>
<?php } else { ?>
<fb:login-button></fb:login-button>
<?php } ?>
<div id="fb-root"></div>
<script>
window.fbAsyncInit = function() {
FB.init({
appId: <?php echo($app_id); ?>,
cookie: true, // enable cookies to allow the server to access the session
oauth: true, // enable OAuth 2.0
xfbml: true // parse XFBML
});
FB.getLoginStatus(function (res) {
console.log(res.status);
});
};
(function(d){
var js, id = 'facebook-jssdk'; if (d.getElementById(id)) {return;}
js = d.createElement('script'); js.id = id; js.async = true;
js.src = "//connect.facebook.net/en_US/all.js";
d.getElementsByTagName('head')[0].appendChild(js);
}(document));
</script>
</body>
</html>
Was mache ich falsch? oder das ist ein Problem mit Facebook?
UPDATE:
Ich habe meinen Code aktualisiert, um den Fluss von @cpilko geschrieben folgen jedoch bin ich immer noch das gleiche Problem. Ich kann mich ein- und ausloggen. Wenn ich jedoch die Testseite nach einigen Stunden besuche, sage ich am nächsten Tag, dass ich die Session expired-Ausnahme mit dem mitgelieferten langlebigen Zugriffstoken bekomme (FB JS SDK denkt, dass ich verbunden bin, aber der Server nicht), aktualisiere ich die Seite und Ich werde sowohl vom Server als auch vom FB JS SDK als eingeloggt angezeigt und der Long Life Token, den ich von Facebook bekomme, ist der gleiche wie der, den ich ursprünglich ausprobiert habe (gespeichert in meinen Cookies). Was ich nicht verstehe, ist, warum ich das Langzeit-Token nicht zum ersten Mal benutzen kann. Aktualisierter Code unten.
<?php
require 'facebook.php';
$app_id = "XXXXXXXXXXXXX";
$app_secret = "XXXXXXXXXXXXXXXXXXXX";
$my_url = "http://swan.magicseaweed.local/facebook/";
$facebook = new Facebook(array(
'appId' => $app_id,
'secret' => $app_secret
));
$valid_user = false;
var_dump($_COOKIE);
echo("<br />");
if (isset($_COOKIE["FB_LONG_AC_TOKEN"]))
{ // Have long term token, attempt to validate.
// Attempt to query the graph:
$graph_url = "https://graph.facebook.com/me?"
. "access_token=" . $_COOKIE["FB_LONG_AC_TOKEN"];
$response = curl_get_file_contents($graph_url);
$decoded_response = json_decode($response);
// If we don't have an error then it's valid.
if (!$decoded_response->error) {
$valid_user = true;
$access_token = $_COOKIE["FB_LONG_AC_TOKEN"];
echo("Have long life token.<br />");
}
else {
// Stored token is invalid.
// Attempt to renew token.
// Exchange short term token for long term.
$ch = curl_init("https://graph.facebook.com/oauth/access_token?client_id=".$app_id."&client_secret=".$app_secret."&grant_type=fb_exchange_token&fb_exchange_token=".$facebook->getAccessToken());
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 5);
curl_setopt($ch, CURLOPT_TIMEOUT, 10);
$data = curl_exec($ch);
curl_close($ch);
$params = null;
parse_str($data, $params);
if (isset($params["access_token"]))
{
$access_token = $params["access_token"];
echo("Got long life token.<br />");
setcookie("FB_LONG_AC_TOKEN", $access_token, time() + (3600 * 24 * 60), "/");
}
else
{ // Clear invalid token.
setcookie("FB_LONG_AC_TOKEN", "false", time() - 3600, "/");
echo("Long life token invalid.<br />");
}
}
}
else if ($facebook->getUser())
{ // Have short term access token.
// Verify short term token is valid still.
try {
// Proceed knowing you have a logged in user who's authenticated.
$user_profile = $facebook->api('/me');
}
catch (FacebookApiException $e) { }
if (is_array($user_profile)) { // Have user.
$valid_user = true;
// Exchange short term token for long term.
$ch = curl_init("https://graph.facebook.com/oauth/access_token?client_id=".$app_id."&client_secret=".$app_secret."&grant_type=fb_exchange_token&fb_exchange_token=".$facebook->getAccessToken());
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 5);
curl_setopt($ch, CURLOPT_TIMEOUT, 10);
$data = curl_exec($ch);
curl_close($ch);
$params = null;
parse_str($data, $params);
if (isset($params["access_token"]))
{
$access_token = $params["access_token"];
echo("Got long life token.<br />");
setcookie("FB_LONG_AC_TOKEN", $access_token, time() + (3600 * 24 * 60), "/");
}
}
}
if ($access_token) {
$facebook->setAccessToken($access_token);
// See if there is a user from a cookie
$user = $facebook->getUser();
if ($user) {
try {
// Proceed knowing you have a logged in user who's authenticated.
$user_profile = $facebook->api('/me');
} catch (FacebookApiException $e) {
echo '<pre>'.htmlspecialchars(print_r($e, true)).'</pre>';
$user = null;
}
}
}
// note this wrapper function exists in order to circumvent PHP’s
//strict obeying of HTTP error codes. In this case, Facebook
//returns error code 400 which PHP obeys and wipes out
//the response.
function curl_get_file_contents($URL) {
$c = curl_init();
curl_setopt($c, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($c, CURLOPT_URL, $URL);
$contents = curl_exec($c);
$err = curl_getinfo($c,CURLINFO_HTTP_CODE);
curl_close($c);
if ($contents) return $contents;
else return FALSE;
}
?>
<!doctype html>
<html xmlns:fb="http://www.facebook.com/2008/fbml">
<head>
<title>Facebook Auth</title>
</head>
<body>
<?php if ($user) { ?>
Your user profile is
<pre>
<?php print htmlspecialchars(print_r($user_profile, true)) ?>
</pre>
<?php } else { ?>
<fb:login-button></fb:login-button>
<?php } ?>
<div id="fb-root"></div>
<script>
window.fbAsyncInit = function() {
FB.init({
appId: <?php echo($app_id); ?>,
cookie: true, // enable cookies to allow the server to access the session
oauth: true, // enable OAuth 2.0
xfbml: true // parse XFBML
});
FB.getLoginStatus(function (res) {
console.log(res.status);
});
};
(function(d){
var js, id = 'facebook-jssdk'; if (d.getElementById(id)) {return;}
js = d.createElement('script'); js.id = id; js.async = true;
js.src = "//connect.facebook.net/en_US/all.js";
d.getElementsByTagName('head')[0].appendChild(js);
}(document));
</script>
</body>
</html>
haben Sie versucht, die App in Ihrem persönlichen Konto zu entfernen und neu zu installieren? –