Skip to content

Open-source social media scheduler built for AI agents and the teams behind them. Create, schedule and publish posts to every network through MCP or the REST API, or write them yourself in a composer your team will love.

fix(facebook): use upload_url + file_url for reel transfer phase

Paulo Castellanoc379c4c14f62
diff --git a/app/Services/Social/FacebookPublisher.php b/app/Services/Social/FacebookPublisher.php
index 6a6123613..648119013 100644
--- a/app/Services/Social/FacebookPublisher.php
+++ b/app/Services/Social/FacebookPublisher.php
@@ -212,36 +212,54 @@ class FacebookPublisher
private function publishReel(string $pageId, string $accessToken, ?string $content, $media): array
{
- // Upload video as reel
- $response = $this->socialHttp()->post("{$this->baseUrl}/{$pageId}/video_reels", [
+ // Phase 1 (start) — graph endpoint returns video_id + upload_url.
+ $startResponse = $this->socialHttp()->post("{$this->baseUrl}/{$pageId}/video_reels", [
'upload_phase' => 'start',
'access_token' => $accessToken,
]);
- if ($response->failed()) {
+ if ($startResponse->failed()) {
Log::error('Facebook reel upload start failed', [
- 'status' => $response->status(),
- 'body' => $this->redactResponseBody($response->body()),
+ 'status' => $startResponse->status(),
+ 'body' => $this->redactResponseBody($startResponse->body()),
]);
- $this->handleApiError($response);
+ $this->handleApiError($startResponse);
}
- $data = $response->json();
- $videoId = data_get($data, 'video_id');
+ $startData = $startResponse->json();
+ $videoId = data_get($startData, 'video_id');
+ $uploadUrl = data_get($startData, 'upload_url');
- // Upload the video file (Facebook accepts URL in video_file_chunk)
- $uploadResponse = $this->socialHttp()->post("{$this->baseUrl}/{$videoId}", [
- 'upload_phase' => 'transfer',
- 'video_file_chunk' => $media->url,
- 'access_token' => $accessToken,
- ]);
+ if (! $videoId || ! $uploadUrl) {
+ Log::error('Facebook reel start did not return video_id/upload_url', [
+ 'body' => $this->redactResponseBody($startResponse->body()),
+ ]);
+ $this->handleApiError($startResponse);
+ }
+
+ // Phase 2 (transfer, hosted-file flow) — POST to upload_url on
+ // rupload.facebook.com with `file_url` in the JSON body and
+ // `Authorization: OAuth …` header. The previous implementation
+ // POSTed to the graph endpoint with a made-up `video_file_chunk`
+ // body field; Facebook accepted the request but never fetched
+ // the video, so finish failed with error_subcode 1363130
+ // ("Video Upload Is Missing").
+ $uploadResponse = $this->socialHttp()
+ ->withHeaders(['Authorization' => "OAuth {$accessToken}"])
+ ->asJson()
+ ->post($uploadUrl, [
+ 'file_url' => $media->url,
+ ]);
if ($uploadResponse->failed()) {
- Log::error('Facebook reel upload transfer failed', ['body' => $this->redactResponseBody($uploadResponse->body())]);
+ Log::error('Facebook reel upload transfer failed', [
+ 'status' => $uploadResponse->status(),
+ 'body' => $this->redactResponseBody($uploadResponse->body()),
+ ]);
$this->handleApiError($uploadResponse);
}
- // Finish and publish the reel
+ // Phase 3 (finish) — publish the reel.
$finishResponse = $this->socialHttp()->post("{$this->baseUrl}/{$pageId}/video_reels", [
'upload_phase' => 'finish',
'video_id' => $videoId,
diff --git a/tests/Feature/Services/Social/FacebookPublisherTest.php b/tests/Feature/Services/Social/FacebookPublisherTest.php
index 3f0a050fb..842a47b4e 100644
--- a/tests/Feature/Services/Social/FacebookPublisherTest.php
+++ b/tests/Feature/Services/Social/FacebookPublisherTest.php
@@ -4,6 +4,7 @@ declare(strict_types=1);
use App\Enums\PostPlatform\ContentType;
use App\Enums\SocialAccount\Platform;
+use App\Exceptions\Social\FacebookPublishException;
use App\Exceptions\TokenExpiredException;
use App\Models\Post;
use App\Models\PostPlatform;
@@ -179,10 +180,12 @@ test('facebook publisher can publish reel', function () {
Http::fake([
'*/page_123/video_reels' => Http::sequence()
- ->push(['video_id' => 'reel_video_123'], 200)
+ ->push([
+ 'video_id' => 'reel_video_123',
+ 'upload_url' => 'https://rupload.facebook.com/video-upload/v25.0/reel_video_123',
+ ], 200)
->push(['id' => 'reel_123', 'success' => true], 200),
- '*/reel_video_123' => Http::response(['success' => true], 200),
- '*' => Http::response('', 200),
+ '*rupload.facebook.com/*' => Http::response(['success' => true], 200),
]);
$result = $this->publisher->publish($this->postPlatform);
@@ -190,6 +193,45 @@ test('facebook publisher can publish reel', function () {
expect($result)->toHaveKey('id');
expect($result['id'])->toBe('reel_123');
expect($result['url'])->toBe('https://www.facebook.com/reel/reel_123');
+
+ // Assert the transfer phase: POST to upload_url (rupload host) with
+ // file_url JSON body and OAuth header — not to the graph endpoint
+ // with the (legacy/wrong) video_file_chunk body param.
+ Http::assertSent(function ($request) {
+ if (! str_contains($request->url(), 'rupload.facebook.com')) {
+ return false;
+ }
+
+ return $request['file_url'] === 'https://example.com/media/2026-01/reel.mp4'
+ && str_starts_with($request->header('Authorization')[0] ?? '', 'OAuth ');
+ });
+});
+
+test('facebook publisher fails reel publish when start does not return upload_url', function () {
+ $this->postPlatform->update(['content_type' => ContentType::FacebookReel]);
+
+ $this->post->update([
+ 'media' => [
+ [
+ 'id' => 'test-media-reel',
+ 'path' => 'media/2026-01/reel.mp4',
+ 'url' => 'https://example.com/media/2026-01/reel.mp4',
+ 'mime_type' => 'video/mp4',
+ 'original_filename' => 'reel.mp4',
+ ],
+ ],
+ ]);
+
+ // Missing upload_url in the start response — should not silently
+ // proceed to a broken transfer (which is what the old code did).
+ Http::fake([
+ '*/page_123/video_reels' => Http::response([
+ 'video_id' => 'reel_video_123',
+ ], 200),
+ ]);
+
+ expect(fn () => $this->publisher->publish($this->postPlatform))
+ ->toThrow(FacebookPublishException::class);
});
test('facebook publisher can publish image story', function () {
@@ -376,10 +418,12 @@ test('facebook publisher cleans up temp files after reel upload', function () {
Http::fake([
'*/page_123/video_reels' => Http::sequence()
- ->push(['video_id' => 'reel_video_cleanup_123'], 200)
+ ->push([
+ 'video_id' => 'reel_video_cleanup_123',
+ 'upload_url' => 'https://rupload.facebook.com/video-upload/v25.0/reel_video_cleanup_123',
+ ], 200)
->push(['id' => 'reel_cleanup_456', 'success' => true], 200),
- '*/reel_video_cleanup_123' => Http::response(['success' => true], 200),
- '*' => Http::response('', 200),
+ '*rupload.facebook.com/*' => Http::response(['success' => true], 200),
]);
$this->publisher->publish($this->postPlatform);