Skip to content

fix: resolve Instagram username via Facebook Graph API#130

Merged
chubes4 merged 1 commit intomainfrom
fix-ig-username-fetch
Apr 26, 2026
Merged

fix: resolve Instagram username via Facebook Graph API#130
chubes4 merged 1 commit intomainfrom
fix-ig-username-fetch

Conversation

@chubes4
Copy link
Copy Markdown
Member

@chubes4 chubes4 commented Apr 26, 2026

Summary

Fixes the studio UI showing @unknown for connected Instagram accounts even when auth is healthy.

Root cause

Our OAuth flow uses Facebook Login (exchange_for_long_lived_token_fb), which produces an FB-flavored access token. The previous username lookup hit graph.instagram.com/{user_id}?fields=username, which rejects those tokens with:

HTTP 400
{"error":{"message":"Invalid OAuth access token - Cannot parse access token","type":"OAuthException","code":190}}

The OAuth callback caught the resulting WP_Error and silently stored username = ''. Every reauth since the FB Login switch has been broken this way, but the failure never surfaced because:

  1. Publishing still worked (publish hits FB Graph endpoints, which accept the token).
  2. The empty username degraded gracefully to @unknown in the React UI.

Changes

  • get_username_from_token() now hits graph.facebook.com/v18.0/{user_id}?fields=username, which accepts both FB Login and legacy IG Basic Display tokens. Verified live: returns {"username":"extrachill"}.
  • get_instagram_user_id_from_facebook_token()resolve_instagram_account_from_facebook_token() — same /me/accounts call, now returns array{id, username} so the OAuth callback can capture the username from the call we already make. Saves a round trip on the happy path.
  • OAuth callback prefers the resolved username and only falls back to get_username_from_token() when missing (legacy IG Basic Display tokens). Lookup failures are now logged via datamachine_log so future regressions surface in debug.log instead of silently rendering @unknown.

Verification

Live probe against production token before merge:

GET graph.facebook.com/v18.0/17841400793710113?fields=username
→ HTTP 200 {"username":"extrachill","id":"17841400793710113"}

Production extrachill.com account already backfilled out-of-band as a one-shot data fix; this PR ensures the next reauth (and all future reauths) populate the field correctly.

Test plan

  • php -l clean
  • ✅ Live FB Graph probe returns username with the same token format the production install uses
  • ⏳ Manual reauth on a staging IG account post-deploy to confirm callback path

…ph.instagram.com, which rejects FB Login tokens with 'Cannot parse access token' and silently stored empty usernames
@chubes4 chubes4 merged commit f8cd2e9 into main Apr 26, 2026
1 check failed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant