使用Python调用Instagram评论接口仅获取自身评论的问题咨询
Great question—let's break down why you're only seeing your own comments and how to fix this.
First off, the https://api.instagram.com/v1/media/{media-id}/comments endpoint you're using is part of the deprecated Instagram Legacy API (v1). This old API has strict limitations on data access, which is why you're restricted to only your own comments. Legacy endpoints are no longer supported for new apps and have severely narrowed functionality these days.
Here's what you need to do to fetch all user comments:
1. Switch to the Instagram Graph API
Instagram now requires using the Instagram Graph API (current latest version is v18.0) for most data retrieval tasks, including pulling all comments on a media post. This is the supported, fully-featured replacement for the old v1 API.
2. Secure the Right Permissions
To access all comments on a post, you'll need specific permissions depending on your account type:
- For Business/Creator Instagram Accounts (linked to a Facebook Page):
pages_show_list(to access your linked Facebook Pages)pages_read_engagement(to retrieve engagement data like comments)
- For Personal Accounts: Convert your account to a Creator or Business account first, then request the
instagram_basicpermission. Note: Private posts will only show comments from users who follow you.
3. Python Implementation Example
Here's a straightforward script using the requests library to fetch all comments via the Graph API:
import requests def fetch_instagram_comments(media_id, access_token): api_url = f"https://graph.facebook.com/v18.0/{media_id}/comments" request_params = { "access_token": access_token, "fields": "id,text,from{id,username},created_time" # Customize fields as needed } response = requests.get(api_url, params=request_params) if response.status_code == 200: return response.json() else: error_msg = response.json().get('error', {}).get('message', 'Unknown error') print(f"Request failed: {error_msg}") return None # Usage example your_media_id = "YOUR_MEDIA_ID_HERE" your_access_token = "YOUR_VALID_ACCESS_TOKEN_HERE" comments_data = fetch_instagram_comments(your_media_id, your_access_token) if comments_data: print("All comments:") for comment in comments_data["data"]: username = comment["from"]["username"] text = comment["text"] print(f"@{username}: {text}")
4. Quick Checks to Avoid Headaches
- Confirm your access token is valid and includes the required permissions (you can use Facebook's Access Token Debugger to verify this).
- If the post is private, make sure your app/account has permission to view it (e.g., you follow the user if it's someone else's private post).
- Double-check that the media ID corresponds to a post linked to the account associated with your access token.
内容的提问来源于stack exchange,提问作者oharr

