I was following Instagram API Basic Display documentation. I’ve created Facebook App, configured Instagram Basic Display, added Test User, Authenticated the Test User using GET request:
https://api.instagram.com/oauth/authorize
?app_id={app-id}
&redirect_uri={redirect-uri}
&scope=user_profile,user_media
&response_type=code
But when I try to request access_token using POST request from the documentation: I receive ERROR 400 with message “You must provide a client_id”. However, documentation says nothing about client_id and Instagram Basic Display doesn’t provide client_id.
What am I doing wrong? Has any of you had similar problem?
2
Answers
I managed to get mine working by using GuzzleHttpClient like this.
Step 1. get the Authorization Code $code
Step 2. Get the short-lived AccessToken
Short-Lived Access tokens are valid for just 1 hour.
Step 3 (optional)
If you want the Long-Lived token, which is valid for 60days, you can immediately exchange the $short_lived_access_token.
long_lived_access_token and expires_in can be saved and when the token has expired after 60 days you can refresh it.
Step 4
Now you can fetch the user media like this.
Bear in mind that the long_lived_access_token expires and before you FETCH you should actually check if the token has expired and if it has, exchange it to get a new one. And the token recycling begins.
//functions
Because the fetchAccessToken function uses the POST method, Adding content-type = application/x-www-form-urlencoded on the headers alone didn’t really work. form_params on the options did the trick for me.
You should make a POST request to https://api.instagram.com/oauth/access_token with the parameters in the body, NOT the Params. Make sure that the “x-www-form-urlencoded” option is enable.
See a more detailed answer here: https://stackoverflow.com/a/60851414/1908112