admin管理员组

文章数量:1122832

I have a task that I need to send a comment from the user to the fan page, for this I wrote the sendComment method

 public void sendComment(String postId, String message) throws Exception{
        String endpoint = ".0/" + postId + "/comments";

        String body = "message=" + URLEncoder.encode(message, StandardCharsets.UTF_8)
                + "&access_token=" + EAAGNO4a7r2wBO3zL....;


        HttpRequest request = HttpRequest.newBuilder()
                .uri(URI.create(endpoint))
                .POST(HttpRequest.BodyPublishers.ofString(body))
                .header("Content-Type", "application/x-www-form-urlencoded")
                .build();

        HttpResponse<String> response = client.send(request, HttpResponse.BodyHandlers.ofString());

        if (response.statusCode() == 200) {
            System.out.println("Comment published: " + response.body());
        } else {
            System.err.println("Failed to publish comment. Response Code: " + response.statusCode());
            System.err.println("Response Body: " + response.body());
        }
    }

if you are using this method to post comments from fan page to fan page then in theory it should work, but if from a user to a fan page it won’t work

when I used the method above using the post id and token of the user from which the comment should be posted then i got this error

Failed to publish comment. Response Code: 400 Response Body: {"error":{"message":"Invalid request.","type":"OAuthException","code":1,"fbtrace_id":"A1rMqwYGGdAl0G4MkBZxfBT"}}

but when i use Graph API Explorer with the same data then the request passes

本文标签: facebookHow to send comments from user to fan page via Java using Graph APIStack Overflow