致命错误:未捕获OAuthException:验证访问令牌时出错

时间:2013-02-28 17:05:31

标签: facebook

require ('facebook.php');

define('FACEBOOK_APP_ID',"My_ID");
define('FACEBOOK_SECRET',"My_Secret");
define('REDIRECT_URI',"My_Website");
define('PERMISSIONS_REQUIRED', "publish_stream");
$facebook = new Facebook(array(
    'appId' => FACEBOOK_APP_ID,
    'secret' => FACEBOOK_SECRET,
    'cookie' => true
));
    $access_token = $facebook->getAccessToken();

    $query = mysql_query("SELECT `title` FROM `tentego_img` WHERE `id` = ".intval($_GET['id'])."");
    $r = mysql_fetch_array($query);
    $link = $_SERVER['SERVER_NAME'].$_SERVER['REQUEST_URI'];

    $facebook->api('/me/feed', 'POST',
                                    array(
                                      'link' => $link,
                                      'message' => $r[0]
                                 ));

错误:致命错误:未捕获OAuthException:必须使用活动访问令牌来查询有关当前用户的信息。

请帮忙。

1 个答案:

答案 0 :(得分:1)

试试getAccessTokenFromCode($_GET['code'],REDIRECT_URI),它应该有效。这个对我有用。 修改后的代码:

require ('facebook.php');

define('FACEBOOK_APP_ID',"My_ID");
define('FACEBOOK_SECRET',"My_Secret");
define('REDIRECT_URI',"My_Website");
define('PERMISSIONS_REQUIRED', "publish_stream");
$facebook = new Facebook(array(
    'appId' => FACEBOOK_APP_ID,
    'secret' => FACEBOOK_SECRET,
    'cookie' => true
));
if(isset($_GET['code']))
{
    $access_token = $facebook->getAccessTokenFromCode($_GET['code'],REDIRECT_URI);
     $facebook->setAccessToken($access_token);
    $query = mysql_query("SELECT `title` FROM `tentego_img` WHERE `id` = ".intval($_GET['id'])."");
    $r = mysql_fetch_array($query);
    $link = $_SERVER['SERVER_NAME'].$_SERVER['REQUEST_URI'];

    $facebook->api('/me/feed', 'POST',
                                    array(
                                      'link' => $link,
                                      'message' => $r[0]
                                 ));
}else
{
    $login_url = $facebook->getLoginUrl($params =array('redirect_uri'=>REDIRECT_URI,'scope' => PERMISSIONS_REQUIRED));
 echo ("<script> top.location.href='".$login_url."'</script>");
}