fix
This commit is contained in:
@@ -19,7 +19,6 @@ import org.springframework.web.reactive.function.client.WebClientResponseExcepti
|
|||||||
import reactor.netty.http.client.HttpClient;
|
import reactor.netty.http.client.HttpClient;
|
||||||
|
|
||||||
import java.time.Duration;
|
import java.time.Duration;
|
||||||
import java.util.ArrayList;
|
|
||||||
import java.util.HashMap;
|
import java.util.HashMap;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
@@ -29,7 +28,7 @@ import java.util.stream.Collectors;
|
|||||||
public class FacebookPostingService {
|
public class FacebookPostingService {
|
||||||
|
|
||||||
private static final Logger logger = LoggerFactory.getLogger(FacebookPostingService.class);
|
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 WebClient webClient;
|
||||||
private final ObjectMapper objectMapper;
|
private final ObjectMapper objectMapper;
|
||||||
@@ -49,6 +48,7 @@ public class FacebookPostingService {
|
|||||||
this.objectMapper = new ObjectMapper();
|
this.objectMapper = new ObjectMapper();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// === NEW METHOD FOR MESSAGING ===
|
||||||
public void sendPrivateMessage(String pageAccessToken, String recipientId, String messageText) {
|
public void sendPrivateMessage(String pageAccessToken, String recipientId, String messageText) {
|
||||||
try {
|
try {
|
||||||
Map<String, Object> recipient = new HashMap<>();
|
Map<String, Object> recipient = new HashMap<>();
|
||||||
@@ -77,7 +77,7 @@ public class FacebookPostingService {
|
|||||||
|
|
||||||
} catch (WebClientResponseException e) {
|
} catch (WebClientResponseException e) {
|
||||||
logger.error("Failed to send message: {}", e.getResponseBodyAsString());
|
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) {
|
private PageCredentials getPageCredentials(String accessToken) {
|
||||||
try {
|
try {
|
||||||
|
// 1. Try to fetch accounts (This fails if token is already a Page Token)
|
||||||
String response = webClient.get()
|
String response = webClient.get()
|
||||||
.uri(uriBuilder -> uriBuilder
|
.uri(uriBuilder -> uriBuilder
|
||||||
.path("/me/accounts")
|
.path("/me/accounts")
|
||||||
@@ -180,6 +181,7 @@ public class FacebookPostingService {
|
|||||||
JsonNode data = objectMapper.readTree(response).get("data");
|
JsonNode data = objectMapper.readTree(response).get("data");
|
||||||
|
|
||||||
if (data.isEmpty()) {
|
if (data.isEmpty()) {
|
||||||
|
// If no pages found, try treating it as a Page Token directly
|
||||||
return getPageDetailsDirectly(accessToken);
|
return getPageDetailsDirectly(accessToken);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -189,9 +191,14 @@ public class FacebookPostingService {
|
|||||||
firstPage.get("access_token").asText());
|
firstPage.get("access_token").asText());
|
||||||
|
|
||||||
} catch (WebClientResponseException e) {
|
} 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);
|
return getPageDetailsDirectly(accessToken);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (isTokenExpiredError(e)) throw parseFacebookError(e);
|
if (isTokenExpiredError(e)) throw parseFacebookError(e);
|
||||||
throw new RuntimeException("Failed to get page credentials", e);
|
throw new RuntimeException("Failed to get page credentials", e);
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
@@ -201,20 +208,23 @@ public class FacebookPostingService {
|
|||||||
|
|
||||||
private PageCredentials getPageDetailsDirectly(String pageAccessToken) {
|
private PageCredentials getPageDetailsDirectly(String pageAccessToken) {
|
||||||
try {
|
try {
|
||||||
|
// Verify the token works for /me and get the Page ID
|
||||||
String response = webClient.get()
|
String response = webClient.get()
|
||||||
.uri(uriBuilder -> uriBuilder
|
.uri(uriBuilder -> uriBuilder
|
||||||
.path("/me")
|
.path("/me")
|
||||||
.queryParam("access_token", pageAccessToken)
|
.queryParam("access_token", pageAccessToken)
|
||||||
.queryParam("fields", "id")
|
.queryParam("fields", "id,name")
|
||||||
.build())
|
.build())
|
||||||
.retrieve()
|
.retrieve()
|
||||||
.bodyToMono(String.class)
|
.bodyToMono(String.class)
|
||||||
.block(Duration.ofMillis(timeoutMs));
|
.block(Duration.ofMillis(timeoutMs));
|
||||||
|
|
||||||
String pageId = objectMapper.readTree(response).get("id").asText();
|
String pageId = objectMapper.readTree(response).get("id").asText();
|
||||||
|
logger.info("Successfully resolved Page Token for Page ID: {}", pageId);
|
||||||
return new PageCredentials(pageId, pageAccessToken);
|
return new PageCredentials(pageId, pageAccessToken);
|
||||||
} catch (Exception e) {
|
} 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);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user