Trouble with PHP SDK 3 and access token
I've developed a set of pages to get permission and authenticate the user with the new PHP SDK, but even if I've read many posts and suggestions and solutions, I can't figure how to solve my trouble.
Generally I have two or more php scripts. The first one, index.php, is the one in which authorization are asked and where i get login url using sdk.
The other pages generally include a file in which I initialize facebook SDK and use some methods (like facebook->api("/me"))
Everything works fine in 90% of use, but sometimes I get errors like "An active access token must be used" (usually when i log out from facebook and then i log in trying to access the application with a different user on the same browser; however this situation is not always reproducible, even if i tried changing my user password - I know that change password is one of the way to invalidate a token).
I'll show you my code in the next lines.
Every help will be appreciated, thank you very much.
CONFIG.PHP
define('FB_APP_ID', 'my app id');
define('FB_SECRET', 'my secret');
define('FB_SCOPE', 'my scope');
define('FB_REDIRECT_URI','http://<my_base_url>/index.php');
define('FB_APP_URL','http://apps.facebook.com/<my_app_name>');
define('FB_REDIRECT_PAGE','page.php');
INDEX.PHP
<?php
include('config.php');
require 'libs/facebook.php';
$facebook = new Facebook(array(
'appId' => FB_APP_ID,
'secret' => FB_SECRET,
));
header('P3P:CP="IDC DSP COR ADM DEVi TAIi PSA PSD IVAi IVDi CONi HIS OUR IND CNT"');
$user = $facebook->getUser();
$scope = FB_SCOPE;
if ($user) {
$logoutUrl = $facebook->getLogoutUrl();
} else {
$loginUrl = $facebook->getLoginUrl(array('scope' => $scope, 'redirect_uri' => FB_REDIRECT_URI开发者_开发技巧));
}
if(isset($loginUrl)) { ?>
<html xmlns:fb="http://www.facebook.com/2008/fbml">
<head>
<title>php-sdk</title>
<script type='text/javascript'>
function doRedirect() {
top.location.href = '<?= $loginUrl; ?>';
}
window.setTimeout("doRedirect()", 1000);
</script>
</head>
<body>
Wait Please ...
</body>
</html>
<?
die();
} else {
?>
<html xmlns:fb="http://www.facebook.com/2008/fbml">
<head>
<title>php-sdk</title>
<script type='text/javascript'>
function doRedirect() {
if (window.top === window.self) {
top.location.href = '<?=FB_APP_URL?>';
}
else {
location.href = '<?=FB_REDIRECT_PAGE?>';
}
}
window.setTimeout("doRedirect()", 1000);
</script>
</head>
<body>
Wait Please ...
</body>
</html>
<?
die();
}
?>
PAGE.PHP
include('config.php');
require 'libs/facebook.php';
$facebook = new Facebook(array(
'appId' => FB_APP_ID,
'secret' => FB_SECRET,
));
header('P3P:CP="IDC DSP COR ADM DEVi TAIi PSA PSD IVAi IVDi CONi HIS OUR IND CNT"');
$user = $facebook->getUser();
try {
$me = $facebook->api('/me');
} catch (Exception $e) {
//header("Location: index.php");
//die();
}
this "page.php" is the one in which i usually get the Oauth exception. I can even catch the Oauth exception, but if i try to redirect to the index.php (in which I think the code I wrote can reissue the access token) an endless loop starts in the index.page (and page.php is never reached).
I read a post about token expiration handling, but currently i have some difficulty in understand where and how edit my codebase.
Thank you in advance
Emanuele
精彩评论