How can I get the user ID with the user name with instagram API without user authentication? If I try users/search, it returns multiple research results, so how can I be sure that I get the result of exact user name only?
For example, following requests return multiple users having their usernames similiar to aliciakeys
, I want to get only the data of the user with exact user name.
https://api.instagram.com/v1/users/search?q=aliciakeys&access_token=[your token]
If you know that the search term will always be the full username and you can simply iterate through the results and stop when the username is an exact match of the search query.
Also, don't ever expose your access tokens in public.
This is how you would do it in PHP
<?php
function getInstaID($username)
{
$username = strtolower($username); // sanitization
$token = "InsertThatHere";
$url = "https://api.instagram.com/v1/users/search?q=".$username."&access_token=".$token;
$get = file_get_contents($url);
$json = json_decode($get);
foreach($json->data as $user)
{
if($user->username == $username)
{
return $user->id;
}
}
return '00000000'; // return this if nothing is found
}
echo getInstaID('aliciakeys'); // this should print 20979117
?>
Actually, chances are that if you are searching for the full username, almost every time you search for it, the first result will be the one you would be looking for.