0

I am trying to upload a photo to user's album with current script:

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

$user->access_token is the user's access token and I check via Facebook Graph API Explorer tool it says still valid (until 2 months).

However if I create API request with script above, my console always returned:

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

What I've been doing is:

  • deauthorize app
  • re-authorize app
  • request permission (publish_stream,photo_upload,user_photos)
  • update FB SDK to latest version

Here is the full code:

    $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);
    }

Any help?

FYI I am using codeigniter. And actually I was trying hours ago (it worked), however, now is not, because of access_token.

Thanks.

mokalovesoulmate
  • 271
  • 2
  • 6
  • 18

1 Answers1

0

I solved it manually by NOT using built-in $facebook->api() but I am using curl instead.

$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);
mokalovesoulmate
  • 271
  • 2
  • 6
  • 18