使用 PHP 将图片上传到 facebook 时出错: OAuthException:必须使用活动访问令牌来查询有关当前


Error uploading picture to facebook using PHP: OAuthException: An active access token must be used to query information about the current user

>我只是尝试使用PHP将图像上传到用户的时间线。安装我的应用程序后,我使用以下代码,但收到错误:OAuthException:必须使用活动访问令牌来查询有关当前用户的信息。

我能够通过$token变量回显访问令牌,因此我知道它在那里。我到处寻找并试图修复它,但没有成功。这是代码...

<?php  
include_once "src/facebook.php";  
$app_id = 'xxxxxx';  
$application_secret = 'xxxxxx';  
$facebook = new Facebook(array(  
'appId'  => $app_id,  
'secret' => $application_secret,  
'cookie' => true, // enable optional cookie support  
));  
if ($facebook->getUser())
{
  $user = $facebook->getUser();
  //die($token);
        try
        {
            $token = $facebook->getAccessToken();
            $facebook->setFileUploadSupport(true);
            $file = "5star.png";
            $post_data = array(
            "message" => "My photo caption",
            "source" => '@' . realpath($file)
            );
            //die("/me/photos/?access_token=$token");
            $data['photo'] = $facebook->api("/me/photos?access_token=$token", 'post', $post_data);
        } catch (FacebookApiException $e)
        {
            die($e);
        }
header( 'Location: http://google.com' ) ;
}else
{
  $loginUrl = "https://graph.facebook.com/oauth/authorize?type=user_agent&display=page&client_id=$app_id&redirect_uri=http://apps.facebook.com/yourprofilerating/&scope=user_photos,email,offline_access,publish_stream,user_birthday,user_location,user_work_history,user_about_me,user_hometown";  
  echo "<script> top.location.href='" . $loginUrl . "'</script>";
}
?>

您可能具有无效或过期的访问令牌,请查看处理无效和过期的访问令牌。在 api 调用之前和声明 $token 之后插入本文中提供的代码。

相关文章: