Skip to content
Advertisement

Refreshing Instagram long-lived access token works in browser (http) but not PHP (curl)

hope you can help.

I’m trying to implement Instagram’s Basic Display API but unfortunately I’m running into problems generating a long-lived access token or refreshing an existing long-lived token using PHP/cURL. It seems I’m far from the only one having this or similar issues.

I followed Facebooks getting-started guide (located here) and was able to generate a short-lived access token, but that’s about as far as I ever got in PHP. The weird thing is, if I call any of the endpoints over HTTP directly, for example in a browser, the call works flawlessly and I either get a long-lived access token from a short one OR I can refresh an existing long-lived one. But it only works in a browser or by using cURL in the terminal.

It seems as though graph.instagram.com is either blocking my web servers IP or it doesn’t like one of the many possible cURL options. I’ve tried many different combinations but to no avail. I’ve even tried running the PHP script from a different web server (different hoster, different locations and IP, so a blocked IP doesn’t seem to be the case) but still no luck.

The error message that I get from graph.instagram.com is:
IGApiException: Invalid OAuth 2.0 Access Token (error code 190)

I’ve spent way more time on this than is feasible and I’m getting absolutely fed up with this. There must be a simple explanation as to why one method works but not the other. Drives me nuts.

What am I missing? Please help … To reiterate, calling the following URL (with an appropriate short-/long-lived access token) works fine in a browser/terminal, but not in PHP cURL. Why? And how do I fix this?

Generating long-lived access token:
https://graph.instagram.com/access_token?grant_type=ig_exchange_token&client_secret={app-secret}&access_token={short-lived-access-token}

Refreshing:
https://graph.instagram.com/refresh_access_token?grant_type=ig_refresh_token&access_token={long-lived token}

Note:
I’ve set the app etc. up correctly as far as I can tell. I’m assuming if I hadn’t I wouldn’t have been able to generate an access code in the first place nor generate a short lived access token, never mind refreshing an existing long-lived one.

Rant:
I hate Facebook. Seriously, nothing is ever straight forward and way more complicated than it has any right to be sigh

Advertisement

Answer

Wow, after spending several days time on this issue, finally posting this question, it only took another mere 40 minutes before I finally found the solution.

For anyone running into similar issues, in my case the problem was http_build_query (which I was using to set the cURL GET params) in conjunction with the fact that both web servers had the following php.ini setting set:
arg_separator.output = "&"

where the default setting is usually:
arg_separator.output = "&"

To solve the issue, either change http_build_query to use & instead of & like so:
http_build_query($yourParams, null, '&');
or simply revert the changed php.ini setting back to it’s original setting (at your own risk)

User contributions licensed under: CC BY-SA
8 People found this is helpful
Advertisement