Facebook PHP SDK验证访问令牌时出错

时间:2011-04-30 08:20:28

标签: php facebook sdk login

从thinkdiff.net tutorial开始我已经构建了一个带有facebook登录/注销链接的简单测试页面(本地环境)。如果登录,我想要回显fb用户API。

我正在使用最新的facebook PHP SDK (v.2.1.2)

它似乎有效,但是当我退出时,我收到了这个例外:

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] => 
)

这是我的测试页代码

$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>";
}

感谢

卢卡

2 个答案:

答案 0 :(得分:5)

我会尝试在注销时手动清除会话。在返回网址上放置一个GET参数,或者使用不同的返回网址,然后执行以下操作:

$facebook->destroySession();

问题是即使在用户注销后会话cookie仍然存在。当用户返回到您的页面时,您尝试使用该过期的会话来发出请求。祝你好运。

答案 1 :(得分:1)

答案很好,你必须先在

之前调用destroySession()
facebook->destroySession();

然后你可以调用getLoginUrl()