Parween Talat
Parween Talat

Reputation: 1

Instagram access token with invalid error

How can I display Instagram images of my account to my website homepage using access token and user id?

I followed this guide and get Instagram access token and user id https://developers.facebook.com/docs/instagram-basic-display-api/getting-started

With this guide I get my long-lived access token

but when I run below code in PHP

$access_token="--elided--";
$photo_count=6;
$json_link="https://api.instagram.com/v1/users/self/media/recent/?";
$json_link.="access_token={$access_token}&count={$photo_count}";
echo json_link;

It shows:

{"meta": {"code": 400, "error_type": "OAuthAccessTokenException", "error_message": "The access_token provided is invalid."}}

How can I show Instagram media on my website?

Upvotes: 0

Views: 2462

Answers (2)

sum_random_scrub
sum_random_scrub

Reputation: 1

Im kind of late but perhabs try to use different API URL. I was also stuck on this problem, trying to use

https://api.instagram.com/v1/users/self/media/recent/?...

But later found this address

https://graph.instagram.com/me/media?fields=media_url,media_type&access_token=...

It works for me with this URL. Other stuff to put into fields=

I would reccomend testing with either curl in cmd or downloading Postman

Also if you followed that guide, you'll end up with short-lived token. There is a way to exchange it for long-lived token in this tutorial

But all you really need to do is get the URL ready

https://graph.instagram.com/access_token?grant_type=ig_exchange_token&
client_secret={client_secret}&
access_token={token}

where

  • client_secret is your instagram secret (developers.facebook.com => my apps => => on the left panel click "Instagram basic display")
  • token is that short-lived token you get from the tutorial mentioned in question (this one)

Upvotes: 0

giorgos
giorgos

Reputation: 398

That guide didn't work for me either. Eventually, I figured this out following the procedure described here.

A few key points to keep in mind:

  • Tokens are being generated from Facebook for Developers , with no need for a post request like before.
  • To fetch the photos, you need to make more than one call to the API: One for fetching the photos' IDs and then, one separate call for each ID to get the actual data (permalink, image URL, caption, etc).
  • There is a limit to the calls that you can make per hour, which, as I realized, can be easily reached on a site with moderate traffic. So, you need to somehow cache the results (on WordPress, I used a transient for that, which seems to work fine).
  • The token expires after 60 days and you need to refresh it on time for your app to keep working.

Upvotes: 0

Related Questions