You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Android应用中使用Graph API获取Facebook帖子完整数据的问题

Fixing Missing Data in Facebook Graph API /me/posts Requests

Hey there! Let's break down why you're missing those key pieces of data and how to fetch everything in a single request.

Why You're Not Getting the Data You Need

The Facebook Graph API works on an "opt-in" basis for nested or extended data fields. By default, when you request /me/posts with basic fields like comments.summary(true), it only returns core comment data (message, author, timestamp) and the summary—but not attachments (like comment images) or comment replies. Similarly:

  • full_picture only returns the primary image for multi-image posts; you need to explicitly request the attachments field to get all media links.
  • The likes field only gives you like data; other reactions (love, haha, etc.) live in the separate reactions field, which you have to request explicitly.

How to Fetch All Data in One Request

You need to update your fields parameter to include nested fields for comments, attachments, and reactions. Here's the adjusted fields string you should use:

params.putString("fields", "message,created_time,id,full_picture,status_type,source,attachments{media},comments{message,created_time,from,id,attachment,comments{message,created_time,from,id}},likes.summary(true),reactions.summary(true)");

Let's break down what each part does:

  • attachments{media}: Pulls all media (images/videos) attached to the post. Each media item will include the image URL under media.image.src.
  • comments{message,created_time,from,id,attachment,comments{...}}:
    • attachment fetches any images attached to the comment.
    • The nested comments{...} fetches replies to each comment (you can nest comments to capture full reply threads!).
  • reactions.summary(true): Gets the full breakdown of reactions (like, love, haha, etc.) along with a summary of total counts for each reaction type.

Optional Tweaks

  • If you only need reaction counts (not individual user data), add .limit(0) to the reactions field: reactions.summary(true).limit(0)—this reduces response size.
  • For deeper reply threads, you can nest comments further, but keep in mind the Graph API has limits on nested depth.

Updated Code Snippet

Here's how your Graph Request code will look with the adjusted fields:

login_button.setReadPermissions(Arrays.asList( "public_profile", "email", "user_birthday", "user_friends","user_posts", "user_status")); // Setting permissions
Bundle params = new Bundle();
// Updated fields parameter with all required nested data
params.putString("fields", "message,created_time,id,full_picture,status_type,source,attachments{media},comments{message,created_time,from,id,attachment,comments{message,created_time,from,id}},likes.summary(true),reactions.summary(true)");
params.putString("limit", "100");
new GraphRequest(
    loginResult.getAccessToken(), 
    "/me/posts", 
    params, 
    HttpMethod.GET, 
    new GraphRequest.Callback() {
        public void onCompleted(GraphResponse response) {
            /* handle the result */
            try {
                EditText postsText = (EditText) findViewById(R.id.postText);
                String res = response.toString();
                res = res.replaceAll("\\{Response:\\s*\\w*\\W*\\s*[0-9]*\\W*\\w*:", "");
                res = res.replaceAll("\\,\\s*error:\\s*null\\W*\\s*","");
                postsText.setText(res);
                writeToFile(res,"response");
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }
).executeAsync();

Quick Test Tip

Before updating your code, use the Facebook Graph API Explorer to run this query and verify the response includes all your required fields:

/me/posts?fields=message,created_time,id,full_picture,status_type,source,attachments{media},comments{message,created_time,from,id,attachment,comments{message,created_time,from,id}},likes.summary(true),reactions.summary(true)&limit=100

This lets you confirm the data structure before integrating it into your app.

内容的提问来源于stack exchange,提问作者RagAnt

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.15 04:52:20