Android应用中使用Graph API获取Facebook帖子完整数据的问题
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_pictureonly returns the primary image for multi-image posts; you need to explicitly request theattachmentsfield to get all media links.- The
likesfield only gives you like data; other reactions (love, haha, etc.) live in the separatereactionsfield, 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 undermedia.image.src.comments{message,created_time,from,id,attachment,comments{...}}:attachmentfetches 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
commentsfurther, 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

