java - 从 RestFB 中的帖子获取用户 ID 评论

标签 java desktop-application restfb

我需要在帖子中发表评论的用户(或者至少是 ID)。

这是一个 Java 桌面应用程序。 我得到access token并选择所有权限,仅用于测试。

我尝试了这种方法,但没有成功:

com.restfb.types.Page fetchedPage = facebookClient.fetchObject(pageName, com.restfb.types.Page.class);
//String pageID = fetchedPage.getId();
//String postID = "some post id from the 'fetchedPage'"
String postParam = "type,from,created_time,message,likes.summary(true),comments.order(chronological).summary(true){from{id}}";
com.restfb.types.Post fetchedPost = facebookClient.fetchObject(pageID + "_" + postID, com.restfb.types.Post.class, com.restfb.Parameter.with("fields", postParam));//the part 'from' of the 'comments' don't work as expected, returns null, that > "comments.order(chronological).summary(true){from{id}}"

com.restfb.types.Comments postComments = fetchedPost.getComments();
if(postComments != null){
    for(com.restfb.types.Comment comment : postComments.getData()){
        if(comment != null){
            //curiously I can not access the user who made the comment, but if it is a page that made a comment in the post it perfectly returns the user (which is a page)
            System.out.println("comment: " + comment.getFrom());// >> 'getFrom()' is null << that is what I need, the or at least the ID as told before.
            com.restfb.types.Comment fetchedComment = facebookClient.fetchObject(pageID + "_" + comment.getId(), com.restfb.types.Comment.class, com.restfb.Parameter.with("fields", "from,id,message,created_time,like_count,comment_count"));
            System.out.println("fetchedComment:  " + fetchedComment);//fetching like that the 'getFrom()' is null too...
            System.out.println();
        }
    }
}

有什么提示吗? 抱歉bad English

最佳答案

如果您只需要评论 ID、发表评论的用户及其姓名,则可以使用此代码。

String postId = pageID + "_" + postID; // you should not generate it, take it from the feed
Connection<Comment> commentConnection = facebookClient.fetchConnection(postId + "/comments", Comment.class,
  Parameter.with("fields", "id,from{name,id}"));

for (List<Comment> commentList : commentConnection) {
  for (Comment comment : commentList) {
    System.out.println("Comment ID: " + comment.getId());
    System.out.println("User ID: " + comment.getFrom().getId());
    System.out.println("User Name: " + comment.getFrom().getName());
  }
}

关于java - 从 RestFB 中的帖子获取用户 ID 评论,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50165097/

相关文章:

java - 使用restfb获取页面访问 token 的正确方法是什么?

java - ActionListener 未附加到 jbutton?

java - 如何在 spring-roo 中定义一个出现在页面上但不持久化的字段

java - 没有 ApplicationListener 的 Libgdx 批处理

node.js - NodeJS 桌面应用程序没有服务器?

java - 使用应用程序访问 token 在 Facebook 页面上发帖

java - 方法重载与工厂

java - TableRow 在 TableLayout 中返回空值?

javascript - 如何使用 HTML/CSS/JavaScript 开发桌面应用程序?

java - 线程 "main"com.restfb.exception.FacebookOAuthException : Received Facebook error response of type OAuthException 中出现异常