Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Instagram API: How to get all user media?

Tags:

api

instagram

In general I need to get all user media.

User has more than 250 photos.

I do /users/1/media/recent/?access_token=...&count=250

But it returns only 20 photos.

Maybe instagram has a limit for getting media. If it is, response has a pagination to solve it. But there are only max ID photo. How to know the first (min) ID photo to paginate it then?

like image 489
Anthony Avatar asked Jun 04 '12 12:06

Anthony


People also ask

How do I get media ID for Instagram API?

Get the permalink URL of the Instagram image you need the media ID for. Run the link in a browser and Instagram will provide all the metadata of the image in JSON, from which you can easily recover the image ID and other valuable data.

What information can I get from Instagram API?

The API can be used to get and publish their media, manage and reply to comments on their media, identify media where they have been @mentioned by other Instagram users, find hashtagged media, and get basic metadata and metrics about other Instagram Businesses and Creators.

What are some of the limitations with Instagram's API?

Instagram gives every third-party application connected to its API a limit of how many times it can call their data. They have now reduced their API limit from 5,000 calls per hour to 200 calls per hour.


2 Answers

You're right, the Instagram API will only return 20 images per call. So you'll have to use the pagination feature.

If you're trying to use the API console. You'll want to first allow the API console to authenticate via your Instagram login. To do this you'll want to select OAUTH2 under the Authentication dropdown.

Once Authenticated, use the left hand side menu to select the users/{user-id}/media/recent endpoint. So for the sake of this post for {user-id} you can just replace it with self. This will then use your account to retrieve information.

At a bare minimum that is what's needed to do a GET for this endpoint. Once you send, you'll get some json returned to you. At the very top of the returned information after all the server info, you'll see a pagination portion with next_url and next_max_id.

next_max_id is what you'll use as a parameter for your query. Remember max_id is the id of the image that is the oldest of the 20 that was first returned. This will be used to return images earlier than this image.

You don't have to use the max_id if you don't want to. You can actually just grab the id of the image where you'd like to start querying more images from.

So from the returned data, copy the max_id into the parameter max_id. The request URL should look something like this https://api.instagram.com/v1/users/self/media/recent?max_id=XXXXXXXXXXX where XXXXXXXXXXX is the max_id. Hit send again and you should get the next 20 photos.

From there you'll also receive an updated max_id. You can then use that again to get the next set of 20 photos until eventually going through all of the user's photos.

What I've done in the project I'm working on is to load the first 20 photos returned from the initial recent media request. I then, assign the images with a data-id (-id can actually be whatever you'd like it to be). Then added a load more button on the bottom of the photo set.

When the button is clicked, I use jQuery to grab the last image and it's data-id attribute and use that to create a get call via ajax and append the results to the end of the photos already on the page. Instead of a button you could just replace it to have a infinite scrolling effect.

Hope that helps.

like image 87
michaellee Avatar answered Sep 26 '22 23:09

michaellee


I've solved this issue with the optional parameter count set to -1.

like image 42
Stefan Avatar answered Sep 23 '22 23:09

Stefan