7

我正在尝试使用 Graph api 更新 Facebook 用户状态。我的代码是

<?php 
    require 'facebook.php'; 
    $facebook = new Facebook(array( 
    'appId' =>'389694921095423',
    'secret' =>'XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX',
    'cookie' => true
    )); 
    $access_token = $facebook->getAccessToken();
    echo($access_token);
    $me = null; 

    try 
    { 
        $uid = $facebook->getUser(); 
        $me = $facebook->api('/me'); 
        echo "Welcome User: " . $me['name'] . "<br />"; 
        //access permission
        $permissions_needed = array('publish_stream', 'read_stream', 'offline_access', 'manage_pages');
        foreach($permissions_needed as $perm) 
        {  
            if( !isset($permissions_list['data'][0][$perm]) || $permissions_list['data'][0][$perm] != 1 )
            {    
            $login_url_params = array(
                'scope' => 'publish_stream,read_stream,offline_access,manage_pages',         
                'fbconnect' =>  1,         
                'display'   =>  "page",         
                'next' => 'http://'.$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI']      
                );    
            $login_url = $facebook->getLoginUrl($login_url_params);   
            header("Location: {$login_url}");
            exit(); 
            }
        }
        //Access permission

        $post_id = $facebook->api("/$uid/feed", "post", array("message"=>"Hello World!")); 
        if(isset($post_id)) 
        {
            echo "A new post to your wall has been posted with id: $post_id"; 
        }
    } 
    catch (FacebookApiException $e) 
    { 
        echo($e); 
    } 
?>

问题是它显示了一个运行时错误,例如致命错误:未捕获的 OAuthException:必须使用活动访问令牌来查询有关当前用户的信息。我已经生成了访问令牌。我必须用它来删除这个异常提前谢谢

4

2 回答 2

32

尝试换行:

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

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

如果您没有使用最新版本,并且如果您刚刚升级了 sdk 并导致了问题,也可以尝试升级您的 sdk,然后您可以尝试降级版本并检查。

于 2012-12-13T11:45:26.123 回答
5

清除缓存和 cookie(如果您正在测试或开发应用程序),请执行以下操作:-

try
{  
  $fb_user_info = $facebook->api('/me');
} catch (Exception $ex)
{
  $facebook->destroySession();
  $params = array(
      'scope' => 'YOUR_PERMISSIONS',
      'redirect_uri' => 'YOUR_SITE_URL'
  );
  $fb_login_url = $facebook->getLoginUrl($params);  
  header("Location:" . $fb_login_url);
}
于 2013-08-15T11:51:04.297 回答