Monday, June 5, 2017

Get user's recent media with account username instead of user ID

Leave a Comment

So I want to use the Instagram API to get a specific user's (NOT MY USER) recent media. Instagram has the following endpoint for doing so:

(GET) https://api.instagram.com/v1/users/{user-id}/media/recent/?access_token=ACCESS-TOKEN 

This is a problem because I do not have the user's ID (why would I, Instagram?). What I do have is the account username (as most people would), e.g. @thisisauser.

I've been reading the API docs and I can't find an endpoint that will give me the user ID for a specific account username. I mean, yes, there is:

(GET) https://api.instagram.com/v1/users/search?q=jack&access_token=ACCESS-TOKEN 

...but it doesn't do what I need it to do, which is to search for an exact match.

I've also checked out other threads on Stack Overflow and other websites. However, the alternative solutions offered are, at best, questionable.

The fact that this whole thing is an actual issue surprises me. I mean, SURELY there's a legitimate, Instagram-approved, precise and straightforward way of either:

  • obtaining a user's recent media by providing the account username (which is what people know you by... not a sequence of numbers) OR
  • obtaining a user's ID via an exact match search, with no chance of multiple possible results

FYI: I'm doing this server-side, using PHP and cURL.

Side note: If I have to make a separate request to the API in order to convert an account username to a user ID, that's just a waste of a request, one more for the hourly limit. Just an observation, in case any member of the Instagram team happens to see this.

3 Answers

Answers 1

I couldn't find anything in the API that gets a username's media. However, by using the search endpoint, you can come up with a solution to get a user's media.

Since you're using PHP cURL, you could do something like this:

//this is what was returned by cURL $response = /*what was returned by the API - see below*/  { "data": [{     "username": "jack",     "first_name": "Jack",     "profile_picture": "http://distillery.s3.amazonaws.com/profiles/profile_66_75sq.jpg",     "id": "66",     "last_name": "Dorsey" }, {     "username": "sammyjack",     "first_name": "Sammy",     "profile_picture": "http://distillery.s3.amazonaws.com/profiles/profile_29648_75sq_1294520029.jpg",     "id": "29648",     "last_name": "Jack" }, {     "username": "jacktiddy",     "first_name": "Jack",     "profile_picture": "http://distillery.s3.amazonaws.com/profiles/profile_13096_75sq_1286441317.jpg",     "id": "13096",     "last_name": "Tiddy" }] }  //username I am looking for $user = 'jack';  //filter array response to find exact username you are looking for $filter = array_filter($response,function($userdata) use ($user){     if($userdata['username'] == $user){         return $userdata;     }   });  //now use ID from filtered array to get media $url = 'https://api.instagram.com/v1/users/' . $filter['id'] . '/media/recent/?access_token=ACCESS-TOKEN'; 

Answers 2

You can get all of a user's information (including the id) by requesting this URL:

https://www.instagram.com/{username}/?__a=1 

For example, this gets you all of the information related to the account taylorswift:

https://www.instagram.com/taylorswift/?__a=1 

Answers 3

you can find user id !!!

like this :

<html> <head> <script src="jquery.js" type="text/javascript" ></script> <script type="text/javascript">   $(document).ready(function(){      $.ajax({             type: 'GET',             url: 'https://api.instagram.com/v1/users/self/?access_token=' + window.location.hash.substring(14),             dataType: 'jsonp'}).done(function(response){                  var userid = response.data.id;              });   }); </script> </head> <body> </body> </html> 

and save in to your data base .then when need find it and use that way to find recent media of user :

https://api.instagram.com/v1/users/{user-id}/media/recent/?access_token=ACCESS-TOKEN 

this way i think is beter. you can send a json with php too.

If You Enjoyed This, Take 5 Seconds To Share It

0 comments:

Post a Comment