I'm trying to use the Instagram API to load the latest post by a particular user to a page. According to the API docs, using only the shortlink for the desired picture, the oEmbed response should contain everything that you'd get if you clicked the "embed" link on the picture itself.
Here's my code:
window.onload = function () {
var insta = new XMLHttpRequest();
var embed = document.getElementById('embed');
insta.open("GET", "https://api.instagram.com/v1/users/user/media/recent/?access_token=my-token");
insta.send();
insta.addEventListener("load", function(){
var jsonResponse = JSON.parse(insta.responseText);
var lastImageLink = jsonResponse.data[0].link;
var shortCode = lastImageLink.split("/p/")[1];
var url = "https://api.instagram.com/oembed?url=http://instagr.am/p/" + shortCode;
var getLatest = new XMLHttpRequest;
getLatest.open("GET", url);
getLatest.send()
getLatest.addEventListener("load", function(){
getLatest = JSON.parse(getLatest.responseText);
embed.innerHTML = getLatest.html;
});
});
};
So I do a GET request to get the link of the last post, take the shortlink suffix from that, then use that shortlink to perform another GET request to get get the embed code via the oEmbed method. However, when I load the page, what I see is just this:
What I've tried:
*I know the API depends on the embeds.js library, so I moved that to the head of the html doc and made it load synchronously. No change in result.
*Loading different pictures apart from the latest post. No change there either.
And that's about all I can think of. Is there any reason I'm not able to get the full embedded post with what I'm doing?
Thanks!