【问题标题】:Getting all wall posts and comments using RestFB使用 RestFB 获取所有墙贴和评论
【发布时间】:2014-03-24 07:56:00
【问题描述】:

起初:我知道有一些类似的主题,我根据它们编写了一些代码(如下所示)。我是 Facebook api 的新手,所以我有点迷路:D
无论如何,我的应用程序必须获取所有墙贴和 cmets,但我无法全部获取。有人可以帮我吗?这是我的方法:

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);
}

这是我获取访问令牌的方式:

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

如果有人能帮助我,我将不胜感激。非常感谢,如果我的英语不完美,请见谅。

【问题讨论】:

    标签: java facebook comments posts restfb


    【解决方案1】:

    如果你想获得所有帖子,你只需要这样做:

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

    Parameter.with("limit", xxx) 适用于少于 200 个帖子。并且不要忘记长期访问令牌!

    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

    【讨论】:

    • 所以不可能收到超过 200 个帖子。或者更准确地说,是比过去 200 年更早的帖子?
    猜你喜欢
    • 2013-04-07
    • 2018-01-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-10-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多