调用Instagram API获取含6条评论的帖子返回空数组,求排查原因
解决Instagram API获取评论返回空数组的问题
1. 确认使用正确的媒体ID
你当前使用的18444882094010647是Instagram公共帖子的ID,但Graph API要求使用Instagram商务账号关联的媒体ID才能获取评论。需先通过商务账号的/me/media端点获取对应帖子的正确ID:
$mediaUrl = "https://graph.instagram.com/me/media?fields=id,permalink&access_token=$access_token"; $mediaData = json_decode(file_get_contents($mediaUrl), true); // 遍历匹配目标帖子的permalink,获取正确媒体ID $correctPostId = ''; foreach($mediaData['data'] as $media) { if($media['permalink'] === 'https://www.instagram.com/p/C9efIjby2Xy/') { $correctPostId = $media['id']; break; } }
用获取到的$correctPostId替换原代码中的$postId后重新请求。
2. 验证Access Token的账号类型
确保你的access token归属Instagram商务账号,而非个人用户登录生成的token。个人用户token即便有相关权限,也无法访问商务账号下的媒体评论。可通过debug接口验证:
$debugUrl = "https://graph.instagram.com/debug_token?input_token=$access_token&access_token=$access_token"; $debugData = json_decode(file_get_contents($debugUrl), true); print_r($debugData['data']['type']); // 输出应为"business"
3. 检查帖子的隐私与评论设置
- 确认目标帖子未开启评论审核(未通过审核的评论不会通过API返回)
- 若帖子来自私密账号,需确保你的商务账号已关注该账号
- 检查帖子是否设置了隐藏评论或限制特定用户查看的规则
4. 补充API请求字段参数
尝试在请求中明确指定需要返回的字段,避免默认字段缺失导致的异常:
$commentUrl = "https://graph.instagram.com/$correctPostId/comments?fields=id,text,username&access_token=$access_token";
内容的提问来源于stack exchange,提问作者Dinesh Karki
相关产品推荐
相关产品推荐

