Instagram how to get my user id from username?

谁都会走 提交于 2019-11-27 16:49:11

Very late answer but I think this is the best answer of this topic. Hope this is useful.

You can get user info when a request is made with the url below:

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

E.g:

This url will get all information about a user who's username is therock

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

Update i June-20-2019, the API is public now. No authentication required.

Update in December-11-2018, I needed to confirm that this endpoint still work. You need to login before sending request to this site because it's not public endpoint anymore. The login step is easy also. This is my demo: https://youtu.be/ec5QhwM6fvc


Update in Apr-17-2018, it's look like this endpoint still working (but its not public endpoint anymore), you must send a request with extra information to that endpoint. (Press F12 to open developer toolbar, then click to Network Tab and trace the request.)

Sorry because I'm too busy. Hope this information help you guys. Hmm, feel free to give me a down-vote if you want.


Update in Apr-12-2018, cameronjonesweb said that this endpoint doesn't work anymore. When he/she trying to access this endpoint, 403 status code return.


estherz

Enter this url in your browser with the users name you want to find and your access token

https://api.instagram.com/v1/users/search?q=[USERNAME]&access_token=[ACCESS TOKEN]

Most of the methods are obsolete since June, 1/2016 api changes

Below worked for me,

  • access instagram on your browser say chrome, safari or firefox.
  • Launch developer tools, go to console option.
  • on command prompt enter below command and hit enter:

    window._sharedData.entry_data.ProfilePage[0].user.id
    

If you are lucky, you will get at first attempt, if not, be patient, refresh the page and try again. keep doing until you see user-id. Good luck!!

Zeeshan Akhter

Easily Get USER ID and User Details

https://api.instagram.com/v1/users/search?q=[ USER NAME ]&client_id=[ YOU APP Client ID ]

For Example:

https://api.instagram.com/v1/users/search?q=zeeshanakhter2009&client_id=enter_your_id

Result:

{"meta":{"code":200},"data":[{"username":"zeeshanakhter2009","bio":"http://about.me/zeeshanakhter","website":"http://zeeshanakhter.com","profile_picture":"http://images.ak.instagram.com/profiles/profile_202090411_75sq_1377878261.jpg","full_name":"Zeeshan Akhter","id":"202090411"}]}

Instead of using the API, one can examine the Instagram userpage to get the id. Example code in PHP:

$html = file_get_contents("http://instagram.com/<username>");
$doc = new DOMDocument();
$doc->loadHTML($html);

$xpath = new DOMXPath($doc);
$js = $xpath->query('//body/script[@type="text/javascript"]')->item(1)->nodeValue;

$start = strpos($js, '{');
$end = strrpos($js, ';');
$json = substr($js, $start, $end - $start);

$data = json_decode($json, true);
$data = $data["entry_data"]["UserProfile"][0];
# The "userMedia" entry of $data now has the same structure as the "data" field
# in Instagram API responses to user endpoints queries

echo $data["user"]["id"];

Of course, this code has to be adapted if Instagram changes its page format.

Currently there is no direct Instagram API to get user id from user name. You need to call the GET /users/search API and then iterate the results and check if the username field value is equal to your username or not, then you grab the id.

I wrote this tool for retrieving Instagram IDs by username: Instagram User ID Lookup.

It utilizes the python-instagram library to access the API and includes a link to the source code (written on Django), which illustrates various implementations of the Instagram API.

Update: Added source code for port to Ruby on Rails.

I tried all the aforementioned solutions and none works. I guess Instagram has accelerated their changes. I tried, however, the browser console method and played around a bit and found this command that gave me the user ID.

window._sharedData.entry_data.ProfilePage[0].graphql.user.id

You just visit a profile's page and enter this command in the console. You might need to refresh the page for this to work though. (I had to post this as an answer, because of my low reputation)

You need to use Instagrams API to convert your username to id.

If I remember correctly you use users/search to find the username and get the id from there

to get your id, make an authenticated request to the Instagram API users/self/feed endpoint. the response will contain, among other data, the username as well as the id of the user.

Most of these answers are invalid after the 6/1/2016 Instagram API changes. The best solution now is here. Go to your feed on instagram.com, copy the link address for any of your pictures, and paste it into the textbox on that page. Worked like a charm.

Working solution without access token as of October-14-2018:

Search for the username:

https://www.instagram.com/web/search/topsearch/?query=<username>

Example:

https://www.instagram.com/web/search/topsearch/?query=therock

This is a search query. Find the exact matched entry in the reply and get user ID from the entry.

sharmaravi

Go to the api console & copy link https://api.instagram.com/v1/users/self in text field and authenticate using your instagram id & password, you will get your id in response

This can be done through apigee.com Instagram API access here on Instagram's developer site. After loging in, click on the "/users/search" API call. From there you can search any username and retrieve its id.

{
"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"
}]}


If you already have an access code, it can also be done like this: https://api.instagram.com/v1/users/search?q=USERNAME&access_token=ACCESS_TOKEN

Although it's not listed on the API doc page anymore, I found a thread that mentions that you can use self in place of user-id for the users/{user-id} endpoint and it'll return the currently authenticated user's info.

So, users/self is the same as an explicit call to users/{some-user-id} and contains the user's id as part of the payload. Once you're authenticated, just make a call to users/self and the result will include the currently authenticated user's id, like so:

{
    "data": {
        "id": "1574083",
        "username": "snoopdogg",
        "full_name": "Snoop Dogg",
        "profile_picture": "http://distillery.s3.amazonaws.com/profiles/profile_1574083_75sq_1295469061.jpg",
        "bio": "This is my bio",
        "website": "http://snoopdogg.com",
        "counts": {
            "media": 1320,
            "follows": 420,
            "followed_by": 3410
        }
}
Diego Machaca

If you are using implicit Authentication must have the problem of not being able to find the user_id

I found a way for example:

Access Token = 1506417331.18b98f6.8a00c0d293624ded801d5c723a25d3ec the User id is 1506417331

would you do a split single seperated by . obtenies to acces token and the first element

I think the best, simplest and securest method is to open your instagram profile in a browser, view source code and look for user variable (ctrl+f "user":{") inside main javascript code. The id number inside user variable should be your id.

This is the code how it looked in the moment of writing this answer (it can, and probably will be changed in future):

"user":{"username":"...","profile_picture":"...","id":"..........","full_name":"..."}},

Here is how you can retrieve your user id from a username:

$url = "https://api.instagram.com/v1/users/search?q=[username]&access_token=[your_token]";
$obj = json_decode(@file_get_contents($url));
echo $obj->data[0]->id;
Rathienth Baskaran

You can do this by using Instagram API ( User Endpoints: /users/search )

how-to in php :

function Request($url) {

    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $url);  
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);  
    curl_setopt($ch, CURLOPT_HEADER, 0);  

    $result = curl_exec($ch);

    curl_close($ch);

    return $result;

}

function GetUserID($username, $access_token) {

    $url = "https://api.instagram.com/v1/users/search?q=" . $username . "&access_token=" . $access_token;

    if($result = json_decode(Request($url), true)) {

        return $result['data'][0]['id'];

    }

}

// example:
echo GetUserID('rathienth', $access_token);

Here is a really easy website that works well for me:

http://www.instaid.co.uk/

Or you can do the following replacing 'username' with your Instagram username

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

Or you can login to your Instagram account and use google dev tools and look at the cookies that have been stored. 'ds_user_id' is your user ID

https://api.instagram.com/v1/users/search?q="[USERNAME]"&access_token=[ACCESS TOKEN]

Please notice the quotation marks.

This does not always return a valid result but more often than non-quoted one:

https://api.instagram.com/v1/users/search?q="self"&count=1&access_token=[ACCESS TOKEN] 

returns user "self" (id: 311176867)

https://api.instagram.com/v1/users/search?q=self&count=1&access_token=[ACCESS TOKEN]

returns user "super_selfie" (id: 1422944651)

Working solution ~2018

I've found that, providing you have an access token, you can perform the following request in your browser:

https://api.instagram.com/v1/users/self?access_token=[VALUE]

In fact, access token contain the User ID (the first segment of the token):

<user-id>.1677aaa.aaa042540a2345d29d11110545e2499

You can get an access token by using this tool provided by Pixel Union.

First Create an Application on Instagram and get Client Id for your application

http://instagram.com/developer/

Now just copy paste following Url into browser window by replacing your Username and your Client Id https://api.instagram.com/v1/users/search?q=[Your-username]&client_id=[Your-Client-Id]

you will get a Json Result containing General Information about your account along with your Numeric user Id

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!