josef.van.niekerk
josef.van.niekerk

Reputation: 12121

Batch upload multiple photos to multiple user accounts

I have an array that looks as follows:

$userImages = array(
    '100000000000001' => array(
        '..../image01.jpg',
        '..../image02.jpg',
        '..../image03.jpg',
    ),
    '100000000000002' => array(
        '..../image04.jpg',
        '..../image05.jpg',
        '..../image06.jpg',
    ),
);

which contains FB user ids as keys, and then an array of images to upload to each users account.

My upload code looks as follows:

/** @var FacebookSessionPersistence $facebook */
$facebook = $this->container->get('fos_facebook.api');
$facebook->setFileUploadSupport(true);

$count = 1;

foreach ($userImages as $userId => $images) {
    $batch = array();
    $params = array();
    foreach ($images as $image) {
        $request = array(
            'method' => 'post',
            'relative_url' => "{$userId}/photos",
            'attached_files' => "file{$count}",
            'access_token' => $this->getUserAccessToken($userId)
        );
        $batch[] = json_encode($request);
        $params["file{$count}"] = '@' . realpath($image);
        $count++;
    }
}
$params['batch'] = '[' . implode(',', $batch) . ']';

$result = $facebook->api('/', 'post', $params);
return $result;

I've added user access tokens to each image, under access_token, but when $facebook-api() is called, I get the following back from Facebook:

enter image description here

Does anyone know why, I'm getting these errors? Am I adding the user access token in the wrong place?

Upvotes: 1

Views: 901

Answers (3)

Kyuzo Nakamaru
Kyuzo Nakamaru

Reputation: 11

Your logic is good, but you need to put the access token inside the body for every individual request.

For example:

...
$request = array(
  'method' => 'post',
  'relative_url' => "{$userId}/photos",
  'attached_files' => "file{$count}",
  'body' => "access_token={$this->getUserAccessToken($userId)}",
);
...

Upvotes: 1

josef.van.niekerk
josef.van.niekerk

Reputation: 12121

The access_token had to be added to the $params associative array, in the root, not to each image item!

Upvotes: 2

C3roe
C3roe

Reputation: 96306

Does anyone know why, I'm getting these errors? Am I adding the user access token in the wrong place?

Have you made sure, you’ve actually added access tokens at all, and not perhaps just a null value?

The error message does not say that you used a wrong or expired user access token, but it says that a user access token is required.

So I’m guessing, because you did not really put actual tokens into your separate batch request parts in the first place, then the fallback to your app access token occurs, and hence that particular error message.

Upvotes: 0

Related Questions