Instagram通过用户名获取user_id的API端点已停用,求替代方法
Hey there! Sorry to hear the old https://api.instagram.com/v1/users/search endpoint got retired—Instagram's been tightening up their API access lately, so this change isn't totally unexpected. Here are your best options to get a user's ID from their username now:
1. Official Instagram Graph API User Search
This is the only compliant, long-term solution, though it requires some setup:
- First, you'll need a Facebook Developer account and an app linked to an Instagram Business or Creator account.
- You'll need a valid access token with required permissions (typically
instagram_basicandpages_show_listfor Page-linked tokens). - Use this endpoint:
GET /ig_user_search?username={TARGET_USERNAME}&access_token={YOUR_VALID_ACCESS_TOKEN} - The response will return an array of matching users, each including the
idfield (your target user ID).
Important: This endpoint only works for Business/Creator accounts, and you can only search for users your app has permission to access (e.g., users following your linked Instagram account, or accounts you've been granted explicit access to).
2. Unofficial Web Scraping (Use with Extreme Caution)
Instagram's web interface exposes user IDs in hidden data, but this violates their Terms of Service and can lead to IP/account blocks:
- Visit the user's profile page (
https://www.instagram.com/{username}/), view the page source, and search for strings likeprofilePage_—the number immediately after this is usually the user ID. - Alternatively, append
?__a=1to the profile URL (e.g.,https://www.instagram.com/{username}/?__a=1) to fetch a raw JSON response. Look for thegraphql.user.idfield in the output.
Warning: Instagram actively targets scrapers, and this unofficial endpoint can break without notice. Only consider this for personal, non-commercial use (and even then, it's risky).
3. Third-Party Tools (Vet Carefully!)
Some third-party services offer user ID lookup by username, but proceed with caution:
- Most rely on the same scraping methods mentioned above, so they carry the same risks of being blocked or shut down.
- Always review the service's privacy policy—never share sensitive access tokens or user data with untrusted providers.
- Many charge fees for higher volume or more reliable access.
Final Notes
- Prioritize the official Graph API whenever possible—it's the only way to stay compliant with Instagram's rules and avoid account restrictions.
- Make sure you understand Instagram's official platform policies before implementing any solution.
内容的提问来源于stack exchange,提问作者Nima

