从RestFB中的帖子获取用户ID注释

时间:2018-05-03 23:29:28

标签: 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

2 个答案:

答案 0 :(得分:0)

您可以通过以下方式获取访问令牌:

  1. 登录您的脸书
  2. 转到您的个人资料页面
  3. 右键点击&#34;查看页面来源&#34;
  4. 使用密钥查找文字:&#34; access_token&#34;

答案 1 :(得分:0)

如果您只需要评论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());
  }
}