From 07fd55f33a950a684c8a0e924ebcb94b35c1418e Mon Sep 17 00:00:00 2001 From: arys Date: Thu, 29 Jan 2026 17:36:13 +0500 Subject: [PATCH] fix --- .../service/FacebookPostingService.java | 22 ++++++++++++++----- 1 file changed, 16 insertions(+), 6 deletions(-) diff --git a/src/main/java/kz/konturai/parser/service/FacebookPostingService.java b/src/main/java/kz/konturai/parser/service/FacebookPostingService.java index a2c5810..ac05413 100644 --- a/src/main/java/kz/konturai/parser/service/FacebookPostingService.java +++ b/src/main/java/kz/konturai/parser/service/FacebookPostingService.java @@ -19,7 +19,6 @@ import org.springframework.web.reactive.function.client.WebClientResponseExcepti import reactor.netty.http.client.HttpClient; import java.time.Duration; -import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; @@ -29,7 +28,7 @@ import java.util.stream.Collectors; public class FacebookPostingService { private static final Logger logger = LoggerFactory.getLogger(FacebookPostingService.class); - private static final String FACEBOOK_GRAPH_API_BASE = "https://graph.facebook.com/v18.0"; + private static final String FACEBOOK_GRAPH_API_BASE = "https://graph.facebook.com/v19.0"; // Updated to v19.0 private final WebClient webClient; private final ObjectMapper objectMapper; @@ -49,6 +48,7 @@ public class FacebookPostingService { this.objectMapper = new ObjectMapper(); } + // === NEW METHOD FOR MESSAGING === public void sendPrivateMessage(String pageAccessToken, String recipientId, String messageText) { try { Map recipient = new HashMap<>(); @@ -77,7 +77,7 @@ public class FacebookPostingService { } catch (WebClientResponseException e) { logger.error("Failed to send message: {}", e.getResponseBodyAsString()); - throw new RuntimeException("Failed to send message: " + e.getMessage(), e); + // Don't throw exception here to avoid failing the whole task if message fails } } @@ -168,6 +168,7 @@ public class FacebookPostingService { private PageCredentials getPageCredentials(String accessToken) { try { + // 1. Try to fetch accounts (This fails if token is already a Page Token) String response = webClient.get() .uri(uriBuilder -> uriBuilder .path("/me/accounts") @@ -180,6 +181,7 @@ public class FacebookPostingService { JsonNode data = objectMapper.readTree(response).get("data"); if (data.isEmpty()) { + // If no pages found, try treating it as a Page Token directly return getPageDetailsDirectly(accessToken); } @@ -189,9 +191,14 @@ public class FacebookPostingService { firstPage.get("access_token").asText()); } catch (WebClientResponseException e) { - if (e.getStatusCode().value() == 400 && e.getResponseBodyAsString().contains("Page")) { + // === FIX IS HERE === + // If we get a 400 Bad Request, it means we cannot list accounts. + // This happens when the token is ALREADY a Page Access Token. + if (e.getStatusCode().value() == 400) { + logger.info("Got 400 on /me/accounts. Assuming token is a Page Token. Trying direct access."); return getPageDetailsDirectly(accessToken); } + if (isTokenExpiredError(e)) throw parseFacebookError(e); throw new RuntimeException("Failed to get page credentials", e); } catch (Exception e) { @@ -201,20 +208,23 @@ public class FacebookPostingService { private PageCredentials getPageDetailsDirectly(String pageAccessToken) { try { + // Verify the token works for /me and get the Page ID String response = webClient.get() .uri(uriBuilder -> uriBuilder .path("/me") .queryParam("access_token", pageAccessToken) - .queryParam("fields", "id") + .queryParam("fields", "id,name") .build()) .retrieve() .bodyToMono(String.class) .block(Duration.ofMillis(timeoutMs)); String pageId = objectMapper.readTree(response).get("id").asText(); + logger.info("Successfully resolved Page Token for Page ID: {}", pageId); return new PageCredentials(pageId, pageAccessToken); } catch (Exception e) { - throw new RuntimeException("Invalid token provided", e); + logger.error("Failed to verify Page Token: {}", e.getMessage()); + throw new RuntimeException("Invalid token provided. Could not fetch Page ID.", e); } }