1

I'm having trouble with something that seems (and theoretically should be) simple: getting the last 20 posts an instagram user made.

Ideally, this could be achieved without even having to create a client. If that's not possible, it should be achieved without requiring the end-user to log into instagram, as I only want to display posts from a particular user regardless of who enters the site and whether or not they're logged in to instagram.

I found a pretty straightforward end-point which provides JSON with the exact data I need (https://www.instagram.com/{username}/media/), but I can't find any reference to it on the docs. Regardless, I tried accessing it through a simple AJAX GET, and received two different errors:

  • No 'Access-Control-Allow-Origin' header is present on the requested resource. when trying the call expecting a JSON response.

  • Unexpected token : when trying the call expecting a JSONP response.

The code is the same in both cases save for the dataType line, and goes as follows:

$.ajax({
    type: 'GET',
    dataType: 'jsonp',
    crossDomain: true,
    url: 'https://www.instagram.com/{username}/media/',
    success: function(data) {
        console.log(data);
    }
});

I searched all over for some kind of answer, but couldn't really find one. I'd like to know at least if the overall method is correct, or if nowadays the /{username}/media node doesn't work for retrieving data and it's just something they just didn't get around to take down / left for informational purpose.

Any help will be greatly appreciated, thanks!

lucasreta
  • 965
  • 2
  • 10
  • 25
  • The response from Instagram does not include CORS headers (hence the 'no access control' error), and it's format is not JSONP (hence the 'unexpected token' error when setting that datatype). Because of this, you cannot make a request to it from JS. You will either need to make the request server side, or use their [API](https://www.instagram.com/developer/) which can be set to return JSONP formatted data – Rory McCrossan Mar 15 '17 at 10:47
  • @RoryMcCrossan I feel stupid having wasted so much time when it could all be solved simply by getting it server-side. Come to think about it, I don't even know why I was so obsessed with getting it through an AJAX call. Thanks, and sorry for posting a duplicate, I was shifting through so many different answers and pages in desperation that I didn't manage to read the ones that were actually helpful. – lucasreta Mar 15 '17 at 10:55
  • No problem - we've all been there :) – Rory McCrossan Mar 15 '17 at 10:56

0 Answers0