问题
starting from a thinkdiff.net tutorial I have built a simple test page (local env) with a facebook login/logout link.If logged in i want to echo out the fb user API.
Im using the latest facebook PHP SDK (v.2.1.2).
It seems to work but when i logout I receive this exception:
FacebookApiException Object
(
[result:protected] => Array
(
[error] => Array
(
[type] => OAuthException
[message] => Error validating access token: The session is invalid because the user logged out or because auth.expireSession was invoked.
)
)
[message:protected] => Error validating access token: The session is invalid because the user logged out or because auth.expireSession was invoked.
[string:Exception:private] =>
[code:protected] => 0
[file:protected] => C:\wamp\www\fb\facebook.php
[line:protected] => 543
[trace:Exception:private] => Array
(
[0] => Array
(
[function] => _graph
[class] => Facebook
[type] => ->
[args] => Array
(
[0] => /me
)
)
[1] => Array
(
[file] => C:\wamp\www\fb\facebook.php
[line] => 492
[function] => call_user_func_array
[args] => Array
(
[0] => Array
(
[0] => Facebook Object
(
[appId:protected] => 1819654718*****
[apiSecret:protected] => a2fccb8e93638b50c8d6b2**********
[session:protected] =>
[signedRequest:protected] =>
[sessionLoaded:protected] => 1
[cookieSupport:protected] => 1
[baseDomain:protected] =>
[fileUploadSupport:protected] =>
)
[1] => _graph
)
[1] => Array
(
[0] => /me
)
)
)
[2] => Array
(
[file] => C:\wamp\www\fb\fb.php
[line] => 33
[function] => api
[class] => Facebook
[type] => ->
[args] => Array
(
[0] => /me
)
)
)
[previous:Exception:private] =>
)
this is my test page code
$fbconfig['appid'] = "18196**********";
$fbconfig['api'] = "5c6910be575e4e688ac6d**********";
$fbconfig['secret'] = "a2fccb8e93638b50c8d6b2**********";
try
{
include_once "facebook.php";
}
catch(Exception $o)
{
echo '<pre>';
print_r($o);
echo '</pre>';
}
// Create our Application instance.
$facebook = new Facebook(array('appId' => $fbconfig['appid'],'secret' => $fbconfig['secret'],'cookie' => true));
$session = $facebook->getSession();
$fbme = null;
// Session based graph API call.
if (!empty($session))
{
d($session);
try
{
$uid = $facebook->getUser();
$fbme = $facebook->api('/me');
}
catch (FacebookApiException $e)
{
d($e);
}
}
function d($d)
{
echo '<pre>';
print_r($d);
echo '</pre>';
}
if ($fbme)
{
$logoutUrl = $facebook->getLogoutUrl();
echo"<a href='{$logoutUrl}'>logout</a>";
d($fbme);
}
else
{
$loginUrl = $facebook->getLoginUrl(array('req_perms' => 'email,read_stream,user_birthday'));
echo"<a href='{$loginUrl}'>login</a>";
}
thanks
Luca
回答1:
I would try manually clearing the session on logout. Put a GET parameter on the return url, or use a different return url, and then do this:
$facebook->destroySession();
The problem is that the session cookie persists even after the user is logged out. When the user returns to your page you are trying to use that expired session to make requests. Good luck.
回答2:
Nice answer, You must call destroySession() first before
facebook->destroySession();
then you could call the getLoginUrl()
来源:https://stackoverflow.com/questions/5840552/facebook-php-sdk-error-validating-access-token