From f062e396eec757b79070e1cb748fdbe56b002cf3 Mon Sep 17 00:00:00 2001 From: michaelschal <35957947-michaelschal@users.noreply.replit.com> Date: Sat, 11 Oct 2025 06:45:34 +0000 Subject: [PATCH] Fix Facebook story posting by implementing a two-step upload process Update `FacebookService.publishStory` to first upload the photo as unpublished to obtain a `photo_id`, and then use this `photo_id` to publish the story. This addresses the issue where stories were not being posted correctly. Replit-Commit-Author: Agent Replit-Commit-Session-Id: ae4037a0-2a6f-4530-9bac-79b543286bda Replit-Commit-Checkpoint-Type: full_checkpoint Replit-Commit-Screenshot-Url: https://storage.googleapis.com/screenshot-production-us-central1/397bca8c-984f-43ff-841a-10897aeb8140/ae4037a0-2a6f-4530-9bac-79b543286bda/d2GBAIk --- .replit | 4 - ...ishing-pos-1760164950123_1760164950124.txt | 90 +++++++++++++++++++ server/services/facebook.ts | 45 +++++++--- 3 files changed, 125 insertions(+), 14 deletions(-) create mode 100644 attached_assets/Pasted-ublishing-post-7e27d1f3-3e85-4d19-b35d-9b4134fce2ce-to-facebook-page-Frouard-Error-publishing-pos-1760164950123_1760164950124.txt diff --git a/.replit b/.replit index cb21dc2..164cd5a 100644 --- a/.replit +++ b/.replit @@ -50,10 +50,6 @@ externalPort = 8081 localPort = 40537 externalPort = 3000 -[[ports]] -localPort = 41569 -externalPort = 9000 - [[ports]] localPort = 41803 externalPort = 8008 diff --git a/attached_assets/Pasted-ublishing-post-7e27d1f3-3e85-4d19-b35d-9b4134fce2ce-to-facebook-page-Frouard-Error-publishing-pos-1760164950123_1760164950124.txt b/attached_assets/Pasted-ublishing-post-7e27d1f3-3e85-4d19-b35d-9b4134fce2ce-to-facebook-page-Frouard-Error-publishing-pos-1760164950123_1760164950124.txt new file mode 100644 index 0000000..b50e052 --- /dev/null +++ b/attached_assets/Pasted-ublishing-post-7e27d1f3-3e85-4d19-b35d-9b4134fce2ce-to-facebook-page-Frouard-Error-publishing-pos-1760164950123_1760164950124.txt @@ -0,0 +1,90 @@ +ublishing post 7e27d1f3-3e85-4d19-b35d-9b4134fce2ce to facebook page Frouard + +Error publishing post 54eabfab-437b-4d84-92aa-b24b9da84f8e: Error: Facebook API error: An unknown error has occurred. (code: 1) + + at FacebookService.publishStory (file:///app/dist/index.js:1997:13) + + at process.processTicksAndRejections (node:internal/process/task_queues:95:5) + + at async FacebookService.publishPost (file:///app/dist/index.js:1824:14) + + at async SchedulerService.publishPost (file:///app/dist/index.js:2072:24) + + at async SchedulerService.processPendingPosts (file:///app/dist/index.js:2034:11) + + at async file:///app/dist/index.js:2010:7 + + at async Timeout._onTimeout (/app/node_modules/node-cron/dist/esm/scheduler/runner.js:70:44) + +Publishing post 7e27d1f3-3e85-4d19-b35d-9b4134fce2ce to facebook page Houdemont + +Error publishing post 1be182d0-00e3-440f-a98c-dadfd45f0304: Error: Facebook API error: An unknown error has occurred. (code: 1) + + at FacebookService.publishStory (file:///app/dist/index.js:1997:13) + + at process.processTicksAndRejections (node:internal/process/task_queues:95:5) + + at async FacebookService.publishPost (file:///app/dist/index.js:1824:14) + + at async SchedulerService.publishPost (file:///app/dist/index.js:2072:24) + + at async SchedulerService.processPendingPosts (file:///app/dist/index.js:2034:11) + + at async file:///app/dist/index.js:2010:7 + + at async Timeout._onTimeout (/app/node_modules/node-cron/dist/esm/scheduler/runner.js:70:44) + +6:40:50 AM [express] GET /api/scheduled-posts 200 in 48ms :: [{"id":"9128ce3e-88b5-4afe-9834-e79e2da… + +6:40:51 AM [express] GET /api/auth/session 304 in 1ms :: {"id":"417b5410-1184-46d7-ac96-e85f731b0c98… + +6:40:51 AM [express] GET /api/scheduled-posts 304 in 35ms :: [{"id":"9128ce3e-88b5-4afe-9834-e79e2da… + +6:40:51 AM [express] GET /api/pages 304 in 40ms :: [{"id":"1abe2ba5-fd7b-423a-b82d-da953c340db2","us… + +6:40:57 AM [express] GET /api/posts/7e27d1f3-3e85-4d19-b35d-9b4134fce2ce 304 in 6ms :: {"post":{"id"… + +Publishing post 7e27d1f3-3e85-4d19-b35d-9b4134fce2ce to facebook page Frouard + +Error publishing post 54eabfab-437b-4d84-92aa-b24b9da84f8e: Error: Facebook API error: An unknown error has occurred. (code: 1) + + at FacebookService.publishStory (file:///app/dist/index.js:1997:13) + + at process.processTicksAndRejections (node:internal/process/task_queues:95:5) + + at async FacebookService.publishPost (file:///app/dist/index.js:1824:14) + + at async SchedulerService.publishPost (file:///app/dist/index.js:2072:24) + + at async SchedulerService.processPendingPosts (file:///app/dist/index.js:2034:11) + + at async file:///app/dist/index.js:2010:7 + + at async Timeout._onTimeout (/app/node_modules/node-cron/dist/esm/scheduler/runner.js:70:44) + +Publishing post 7e27d1f3-3e85-4d19-b35d-9b4134fce2ce to facebook page Houdemont + +Error publishing post 1be182d0-00e3-440f-a98c-dadfd45f0304: Error: Facebook API error: An unknown error has occurred. (code: 1) + + at FacebookService.publishStory (file:///app/dist/index.js:1997:13) + + at process.processTicksAndRejections (node:internal/process/task_queues:95:5) + + at async FacebookService.publishPost (file:///app/dist/index.js:1824:14) + + at async SchedulerService.publishPost (file:///app/dist/index.js:2072:24) + + at async SchedulerService.processPendingPosts (file:///app/dist/index.js:2034:11) + + at async file:///app/dist/index.js:2010:7 + + at async Timeout._onTimeout (/app/node_modules/node-cron/dist/esm/scheduler/runner.js:70:44) + +6:41:03 AM [express] GET /api/auth/session 304 in 1ms :: {"id":"417b5410-1184-46d7-ac96-e85f731b0c98… + +6:41:03 AM [express] GET /api/scheduled-posts 304 in 42ms :: [{"id":"9128ce3e-88b5-4afe-9834-e79e2da… + +6:41:03 AM [express] GET /api/pages 304 in 24ms :: [{"id":"1abe2ba5-fd7b-423a-b82d-da953c340db2","us… + +6:41:07 AM [express] GET /api/stats 200 in 22ms :: {"scheduledPosts":2,"scheduledPostsChange":"+100%… + diff --git a/server/services/facebook.ts b/server/services/facebook.ts index b5ed12f..8cd7201 100644 --- a/server/services/facebook.ts +++ b/server/services/facebook.ts @@ -270,27 +270,52 @@ export class FacebookService { } private async publishStory(post: Post, page: SocialPage, media: Media): Promise { - // Use original URL - Facebook handles the story cropping + // Stories require a 2-step process: + // Step 1: Upload photo as unpublished to get photo_id + // Step 2: Publish the photo as a story using the photo_id + const photoUrl = media.originalUrl; - const params = new URLSearchParams({ + // Step 1: Upload photo as unpublished + const uploadParams = new URLSearchParams({ access_token: page.accessToken!, url: photoUrl, + published: 'false', // Important: must be unpublished first }); - const url = `${this.baseUrl}/${page.pageId}/photo_stories?${params.toString()}`; - - const response = await fetch(url, { + const uploadUrl = `${this.baseUrl}/${page.pageId}/photos?${uploadParams.toString()}`; + + const uploadResponse = await fetch(uploadUrl, { method: 'POST', }); - if (!response.ok) { - const error = await response.json() as FacebookError; - throw new Error(`Facebook API error: ${error.error.message} (code: ${error.error.code})`); + if (!uploadResponse.ok) { + const error = await uploadResponse.json() as FacebookError; + throw new Error(`Facebook API error uploading photo for story: ${error.error.message} (code: ${error.error.code})`); } - const data = await response.json() as FacebookPhotoResponse; - return data.id; + const uploadData = await uploadResponse.json() as FacebookPhotoResponse; + const photoId = uploadData.id; + + // Step 2: Publish the photo as a story + const storyParams = new URLSearchParams({ + access_token: page.accessToken!, + photo_id: photoId, + }); + + const storyUrl = `${this.baseUrl}/${page.pageId}/photo_stories?${storyParams.toString()}`; + + const storyResponse = await fetch(storyUrl, { + method: 'POST', + }); + + if (!storyResponse.ok) { + const error = await storyResponse.json() as FacebookError; + throw new Error(`Facebook API error publishing story: ${error.error.message} (code: ${error.error.code})`); + } + + const storyData = await storyResponse.json() as { success: boolean; post_id: string }; + return storyData.post_id; } }