Skip to content

Instantly share code, notes, and snippets.

@lkLeonov
Created July 28, 2016 17:52
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save lkLeonov/af6de2144e687a86f0fe248d3f219c8d to your computer and use it in GitHub Desktop.
Save lkLeonov/af6de2144e687a86f0fe248d3f219c8d to your computer and use it in GitHub Desktop.
How to get media from instagram via XHR
function getMedia(instaData, mediaCount, callback) {
var profileData = instaData.entry_data.ProfilePage[0];
var userID = profileData.user.id;
var totalMedia = profileData.user.media.count;
var mediaCount = mediaCount > totalMedia ? totalMedia : mediaCount;
var csrf_token = instaData.config.csrf_token;
var xhrBody = "ig_user(" + userID + ") { media.after(0, " + mediaCount + ") {nodes {display_src }}}";
var xhr = new XMLHttpRequest();
xhr.open("POST", '/query/', true)
xhr.setRequestHeader("X-CSRFToken", csrf_token);
xhr.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
xhr.onreadystatechange = function() {
if (this.readyState != 4) return;
callback(this.responseText);
}
xhr.send("q=" + encodeURIComponent(xhrBody));
}
// I want 20 instagram posts from this user
getMedia(window._sharedData, 20, function(data){
console.log(JSON.parse(data));
});
@ALIDerz
Copy link

ALIDerz commented Nov 15, 2021

هل يعمل ؟

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment