Files
marketing-parser/Technical Task: Fix Facebook Graph API Image Upload (403 Forbidden).md
T
2025-12-07 17:24:25 +05:00

2.9 KiB

Technical Task: Fix Facebook Graph API Image Upload (403 Forbidden)

Objective: Modify FacebookPostingService.java to resolve the 403 Forbidden error caused by using a User Access Token for image uploads. Implement a mechanism to automatically exchange the User Token for a Page Access Token before attempting the upload.

Context: The application is currently failing with this error: (#200) This endpoint is deprecated since the required permission publish_actions is deprecated

This happens because we are trying to post to /{userId}/photos or using a User Token on a Page endpoint. Facebook requires a Page Access Token to post content. The user has valid permissions (pages_read_engagement, pages_manage_posts), but the code is not fetching the correct token type.

Required Changes:

  1. Implement Token Exchange Method: Create a helper method getPageCredentials(String userAccessToken) that:

    • Calls GET /me/accounts?access_token={userToken}.
    • Parses the JSON response to find the first available Page.
    • Extracts the id (Page ID) and access_token (Page Token).
    • Returns these credentials.
  2. Update postToFacebookWithImage:

    • Remove the logic that calls getPageId (which only returns the User ID or Page ID without a token).
    • Instead, call the new getPageCredentials.
    • Use the returned Page Token and Page ID for the subsequent /photos upload request.
  3. Refactor publishPostWithImage:

    • Ensure the access_token query parameter uses the Page Token, not the User Token passed into the service originally.
    • Keep the ByteArrayResource filename override (critical for multipart uploads).

Code Reference (Implementation Details):

Please replace the existing logic in FacebookPostingService.java with the following flow:

// 1. New Helper Class
private static class PageCredentials {
    String pageId;
    String pageToken;
    public PageCredentials(String pageId, String pageToken) {
        this.pageId = pageId;
        this.pageToken = pageToken;
    }
}

// 2. New Method to fetch Page Token
private PageCredentials getPageCredentials(String userAccessToken) {
    // Call https://graph.facebook.com/v18.0/me/accounts
    // Return the first page found in the "data" array
}

// 3. Updated Main Method
public String postToFacebookWithImage(String userAccessToken, ...) {
    // Step A: Exchange tokens
    PageCredentials creds = getPageCredentials(userAccessToken);

    // Step B: Upload using creds.pageToken and creds.pageId
    return publishPostWithImage(creds.pageToken, creds.pageId, ...);
}

Acceptance Criteria:

  • The service must successfully fetch a Page Access Token using the provided User Access Token.
  • The image upload request must target /{pageId}/photos.
  • The upload request must use the Page Access Token.
  • The 403 Forbidden error regarding publish_actions must no longer appear.