Facebook OAuth致命错误:未捕获OAuthException:活动访问令牌

时间:2011-03-29 13:13:23

标签: cakephp facebook facebook-graph-api

当我尝试使用Facebook Connect时,我收到此错误。

Fatal error: Uncaught OAuthException: An active access token must be used to query information about the current user. thrown in /home/bakasura/public_html/ginger/app/vendors/facebook/facebook.php on line 543

Facebook行动

function facebook($authorize = null) {
    App::import('Vendor', 'facebook');

$facebook = new Facebook(array(
                        'appId'=> $this->__fbAppId,
                        'secret'=> $this->__fbSecret,
                        'cookie'=>true
                        ));

$session = $facebook->getSession();

if(!empty($session) && ($authorize == null)){
    $login_url = $facebook->getLoginUrl(array(
                            'next' => Router::url(array('action' => 'facebook', 'authorize'), true),
                            'cancel_url' => Router::url(array('action' => 'login'), true),
                            'req_perms' => 'email,publish_stream',
                 ));

    header("Location:".$login_url);
}

$userInfo = $facebook->api('/me');

if(!$userInfo){
    $this->Session->setFlash('Facebook login failed');
    $this->redirect(array('action' => 'login'));
}


$user = array(
    'User' => array(
        'first_name'       => $userInfo['first_name'],
        'last_name'        => $userInfo['last_name'],
        'username'        => $userInfo['username'],
        'email'           => $userInfo['email'],
        'user_group_id'   => 1,
        'status' => 'active'
    ),
    'Oauth' => array(
        'provider'        => 'facebook',
        'provider_uid'    => $userInfo['id']
    )
);

$this->oauthLogin($user);        
}

1 个答案:

答案 0 :(得分:2)

如果未设置会话,则需要重定向到身份验证页面。我是这样做的。

    if(!empty($session) && ($authorize == null)){ 
    //blablabla
    }
     else {
echo "<script type='text/javascript'>top.location.href = '".$this->_facebook->getLoginUrl(array('req_perms' => 'email,read_stream,publish_stream'))."';</script>";      
    }