Instagram API Fix - Following Official Meta Documentation
🔧 Issue Fixed
Updated getLinkedIgAccounts() method to follow the official Meta documentation pattern for retrieving Instagram Business Accounts linked to Facebook Pages.
📚 Official Documentation
Following: Instagram API with Facebook Login - Get Started
🔄 What Changed
Previous Implementation (Incorrect)
// Single query trying to get nested instagram_business_account
const url = new URL("https://graph.facebook.com/v18.0/me/accounts")
url.searchParams.set("fields", "instagram_business_account{id,username}")
Problem: This approach doesn't reliably return Instagram accounts, especially when:
- Permissions aren't properly granted
- Instagram account isn't linked
- API version differences
New Implementation (Following Official Docs)
Step 1: Get all Facebook Pages
GET /me/accounts
Step 2: For each page, query for Instagram Business Account
GET /{page-id}?fields=instagram_business_account{id,username}
This two-step approach is the official recommended pattern from Meta.
📊 Code Changes
File: src/modules/social-provider/instagram-service.ts
async getLinkedIgAccounts(userAccessToken: string) {
// Step 1: Get all pages the user manages
const pagesUrl = new URL("https://graph.facebook.com/v18.0/me/accounts")
pagesUrl.searchParams.set("access_token", userAccessToken)
const pagesResp = await fetch(pagesUrl.toString())
const pagesData = await pagesResp.json()
const pages = pagesData.data || []
const igs = []
// Step 2: For each page, check if it has a linked Instagram Business Account
for (const page of pages) {
const igUrl = new URL(`https://graph.facebook.com/v18.0/${page.id}`)
igUrl.searchParams.set("fields", "instagram_business_account{id,username}")
igUrl.searchParams.set("access_token", userAccessToken)
const igResp = await fetch(igUrl.toString())
const igData = await igResp.json()
if (igData.instagram_business_account?.id) {
igs.push({
id: igData.instagram_business_account.id,
username: igData.instagram_business_account.username,
page_id: page.id,
})
}
}
return igs
}
✅ Benefits
1. Follows Official Pattern
- Matches Meta's documented approach exactly
- More reliable and future-proof
- Better error handling per page
2. Better Error Handling
- If one page fails, others still process
- Individual page errors are logged but don't break the flow
- More resilient to API changes
3. Clearer Debugging
- Two-step process is easier to debug
- Can see exactly which page has issues
- Better logging at each step
4. More Robust
- Works with different permission combinations
- Handles edge cases better
- More tolerant of API version differences
🎯 Required Permissions
According to official docs, you need:
instagram_basic- Access basic Instagram account infopages_show_list- List Facebook Pages
Update your .env:
FACEBOOK_SCOPE=pages_show_list,pages_manage_posts,pages_read_engagement,instagram_basic,instagram_content_publish
📋 Testing Steps
Step 1: Ensure Prerequisites
- ✅ Instagram account is Business or Creator type
- ✅ Instagram account is linked to Facebook Page
- ✅ You have admin access to the Facebook Page
- ✅ Facebook app has
instagram_basicpermission
Step 2: Re-authenticate FBINSTA Platform
- Go to Settings → Social Platforms → FBINSTA
- Click "Access" or "Connect"
- Click "Log in with FBINSTA"
- Grant all permissions (especially Instagram permissions)
Step 3: Check Server Logs
After authentication, you should see:
[OAuth Callback] Fetched 1 Facebook pages and 1 Instagram accounts
If you see:
[OAuth Callback] Fetched 1 Facebook pages and 0 Instagram accounts
Then the Instagram account still isn't linked to the Facebook Page.
Step 4: Verify Data
Check the platform's api_config.metadata:
{
"pages": [
{
"id": "747917475065823",
"name": "Cici Label"
}
],
"ig_accounts": [
{
"id": "17841405822304914",
"username": "cicilabel",
"page_id": "747917475065823"
}
]
}
Step 5: Test Post Creation
- Go to Social Posts → Create
- Select FBINSTA platform
- Instagram Account dropdown should now show your account!
🐛 Debug Endpoint
Use the debug endpoint to test:
GET /admin/socials/debug-instagram?platform_id=YOUR_PLATFORM_ID
This will show:
- Raw API responses from both steps
- Parsed Instagram accounts
- Detailed diagnostics
- Specific error messages if any
🔍 Common Issues & Solutions
Issue 1: Still No Instagram Accounts After Fix
Check:
-
Is Instagram account actually linked to Facebook Page?
- Go to Facebook Page Settings → Instagram
- Should show your Instagram username
-
Is Instagram account Business/Creator type?
- Open Instagram app → Profile → Edit Profile
- Should show "Professional account"
-
Are permissions granted?
- Check OAuth scope includes
instagram_basic - Re-authenticate if needed
- Check OAuth scope includes
Issue 2: Permission Errors
Error: (#200) Requires instagram_basic permission
Solution:
- Update
FACEBOOK_SCOPEin.envto includeinstagram_basic - Re-authenticate the platform
Issue 3: Page Not Found
Error: (#803) Cannot query users by their username
Solution:
- This error means you're trying to query a personal Instagram account
- Convert to Business account first
📊 API Flow Diagram
User clicks "Log in with FBINSTA"
↓
Facebook OAuth (with instagram_basic permission)
↓
Exchange code for access token
↓
Step 1: GET /me/accounts
↓
Returns: [{ id: "page_123", name: "My Page" }]
↓
Step 2: For each page:
GET /page_123?fields=instagram_business_account{id,username}
↓
Returns: { instagram_business_account: { id: "ig_456", username: "myaccount" } }
↓
Store in platform.api_config.metadata:
{
pages: [...],
ig_accounts: [{ id: "ig_456", username: "myaccount", page_id: "page_123" }]
}
↓
Done! ✅
🎉 Expected Outcome
After this fix and re-authentication:
- ✅ Instagram accounts will be fetched correctly
- ✅ They'll appear in the post creation form
- ✅ You can select them when creating FBINSTA posts
- ✅ Publishing to both platforms will work
📝 Next Steps
- Re-authenticate your FBINSTA platform
- Check logs to verify Instagram accounts were fetched
- Test post creation to confirm accounts appear
- Publish a test post to both platforms
If Instagram accounts still don't appear after re-authentication, use the debug endpoint to see the raw API responses and identify the specific issue.