i'm trying use instagram api load latest post particular user page. according api docs, using shortlink desired picture, oembed response should contain you'd if clicked "embed" link on picture itself.
here's 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 request link of last post, take shortlink suffix that, use shortlink perform request get embed code via oembed method. however, when load page, see this:
what i've tried:
*i know api depends on embeds.js library, moved head of html doc , made load synchronously. no change in result.
*loading different pictures apart latest post. no change there either.
and that's can think of. there reason i'm not able full embedded post i'm doing?
thanks!
you need include conditionals inside insta
callback function:
insta.onload = function() { if (insta.readystate === 4) { if (insta.status === 200) { //do stuff } } }
readystate
lets know state request in. can see here, there 5 possible values, last of 4, or done. don't want callback run until it's reached final state. second conditional checking type of server response. if it's 200 (success), want execute success code. need check 404 , damage control if server responds page not found error.
also, in line 11, make sure declaring variable: constructor needs parentheses: var getlatest = new xmlhttprequest();
Comments
Post a Comment