访问令牌在2个月内仍然有效,但API的回复是#2500


Access Token still valid for 2 months but API response says #2500

我正在尝试用当前脚本将照片上传到用户的相册:

$this->facebook->api('/me/photos?access_token='.$user->access_token, 'post', $args);

$user->access_token是用户的访问令牌,我通过Facebook Graph API Explorer工具检查它是否仍然有效(直到2个月)。

然而,如果我用上面的脚本创建API请求,我的控制台总是返回:

Fatal error:  Uncaught OAuthException: An active access token must be used to query information about the current user.

我一直在做的是:

  • 取消对应用程序的授权
  • 重新授权应用程序
  • 请求权限(publish_stream、photo_upload、user_photos)
  • 将FB SDK更新到最新版本

这是完整的代码:

    $user = $this->User_model->get_access_token($this->input->post('fb_id'));
    foreach($this->input->post('media') as $media_id)
    {
        $media = $this->Media_model->get_media($media_id);
        $args = array('message' => 'Photo Caption');
        $args['image'] = '@' . realpath(FCPATH.'uploads/booth_1/'.$media->file);
        $data = $this->facebook->api('/me/photos?access_token='.$user->access_token, 'post', $args);
        print_r($data);
    }

有什么帮助吗?

仅供参考,我正在使用代码点火器。事实上,我几个小时前就在尝试(它奏效了),但现在不行了,因为access_token。

谢谢。

我手动解决了这个问题,没有使用内置的$facebook->api(),而是使用curl。

$filename = '@' . realpath(FCPATH.'uploads/'.$media);
$url = "https://graph.facebook.com/".$user->fb_id."/photos";
$ch = curl_init($url);
$attachment =  array('access_token' => $user->access_token,
                'app_id'            => APP_ID,
                'name'              => "Upload",
                'fileUpload'        => true,
                'message'           => "Message",
                'image'             => $filename,
          );
curl_setopt($ch, CURLOPT_URL,$url);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $attachment);
curl_setopt($ch, CURLOPT_VERBOSE, true);
curl_setopt($ch, CURLOPT_CAINFO, PATH TO FB SDK CERT FOR SSL);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 2);
$result = curl_exec($ch);
if ($result === FALSE) {
    die("Curl failed: " . curl_error($ch));
}
curl_close ($ch);