简体   繁体   English

使用RestFB获取所有留言和评论

[英]Getting all wall posts and comments using RestFB

At first: I know there were some similar topics and I wrote some code according to them (as seeing below). 一开始:我知道有一些类似的主题,并且我根据它们写了一些代码(如下所示)。 I'm new in Facebook api so I'm a little bit lost :D 我是Facebook api的新手,所以我有点迷失了:D
Anyway, my app has to get all wall posts and comments but I'm not able to get it all. 无论如何,我的应用程序必须获取所有留言和评论,但我无法全部获取。 Could anyone help me please? 有人可以帮我吗? Here are my methods: 这是我的方法:

public static void getFacebookPosts(String url){            
            try{
                  LoggedFacebookClient client = new LoggedFacebookClient();

                  Page page = client.fetchObject(url, Page.class);  
                  System.out.println(page.getName());
                  Connection<Post> pageFeed = client.fetchConnection(page.getId() + "/feed", Post.class);

            //Getting posts:
                  for (List<Post> feed : pageFeed){
                        for (Post post : feed){     
                             //PRINTING THE POST 
                             getAllPostComments(post.getId(), client);
                        }
                   }
            }catch(com.restfb.exception.FacebookOAuthException ex){
                  System.out.println("\n!!!!!!! Token Expired !!!!!!!!\n");
            }
}

private static void getAllPostComments(String postId, DefaultFacebookClient client){
            int currentCount = 0;
            JsonObject jsonObject = client.fetchObject(postId + "/comments", JsonObject.class, 
                  Parameter.with("summary", true), Parameter.with("limit", 1));
            long commentsTotalCount = jsonObject.getJsonObject("summary").getLong("total_count");

            System.out.println("\nComments:");
            boolean pom = true;
            while(pom == true){       //There should be "while(currentCount < commentsTotalCount)" but currentCount is always < then commentsTotalCount. That's the problem :)
                  pom = false;
                  Connection<Comment> comments = client.fetchConnection(postId + "/comments", 
                        Comment.class, Parameter.with("limit", 50000), Parameter.with("offset", currentCount));

                  for(Comment komentar : comments.getData()){
                        pom = true;
                        currentCount++;
                        stazenychKomentu++;
                        String mess = komentar.getMessage().replaceAll("\n", " ").replaceAll("\r", " ");
                        System.out.println("    [" + currentCount + "]: " + komentar.getFrom().getName() + " ## " + mess);
                  }
            }
            celkemKomentu += commentsTotalCount;
            System.out.println(currentCount + " / " + commentsTotalCount);
}

and here is the way I get the acces token: 这是我获取acces令牌的方法:

public LoggedFacebookClient(){
       super();
       AccessToken accessToken = this.obtainAppAccessToken(API_KEY, APP_SECRET);
       this.accessToken = accessToken.getAccessToken();
}

I would be very grateful if anyone could help me. 如果有人可以帮助我,我将不胜感激。 Thanks a lot and sorry if my English isn't perfect. 非常感谢,如果我的英语不够完美,请对不起。

If you want to get all posts you just need to make this: 如果您想获取所有帖子,则只需执行以下操作:

    while (pageFeed.hasNext()) {
    pageFeed = facebookClient.fetchConnectionPage(pageFeed.getNextPageUrl(),Post.class);
    }

Parameter.with("limit", xxx) work for less then 200 posts. Parameter.with(“ limit”,xxx)工作少于200个帖子。 And don't forget about long-live access token! 并且不要忘记长寿命访问令牌!

GET /oauth/access_token?  
grant_type=fb_exchange_token&           
client_id={app-id}&
client_secret={app-secret}&
fb_exchange_token={short-lived-token}

https://developers.facebook.com/docs/facebook-login/access-tokens#pagetokens https://developers.facebook.com/docs/facebook-login/access-tokens#pagetokens

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM