Get all Instagram posts using Facebook Oembed

1.3k views Asked by At

I've followed the guide here https://developers.facebook.com/docs/plugins/oembed/ and it works.

My question is, how can I get my post urls dynamically? so I can repeatedly use this endpoint for the number of posts I have

https://graph.facebook.com/v8.0/instagram_oembed?url={postUrl}&access_token={access_token}

The way the docs show is to hard code a url into the endpoint url, which seems redundant

1

There are 1 answers

0
kennsorr On

There is currently no way to get all post URLs for an Instagram profile using the oEmbed API based on the documentation (or anywhere else on the web). You'll have to get all of the post shortcodes (links) by:

  1. Do a GET request to https://www.instagram.com/{profile_handle}/?__a=1. There is a very strict rate limit these days, so it is highly recommended to cache the results the first time, maybe with local storage.
  2. The result will be a JSON object with all of the profile info if the profile is public. You can check of the requests profile is public by checking the [data].graphql.user.is_private
  3. For each [data].graphql.user.edge_owner_to_timeline_media.edges.node, get the shortcode property.
  4. Finally, create your dynamic post URI https://www.instagram.com/p/{shortcode}.

Alternatively, you could do a GET request to `https://www.instagram.com/{profile_handle} and use regex to get the a similar JSON object containing profile data in the HTML response like so:

const uri = `https://www.instagram.com/${handle}/`;
const instaRes = await axios.get(uri);
const html = instaRes.data;
const regex = /_sharedData = (.*);<\/script>/m;
const json = JSON.parse(regex.exec(html)[1]);

The rate limit is strict here too so use be sure to cache. Testing can be a pain because you may have to change your IP address a couple times when you hit the rate limit.