How to figure out an Instagram username from the user id
Asked Answered
S

2

5

I am working on a program that needs to scrape information from the public Instagram API.

My code is using the endpoint https://instagram.com/{username}/?__a=1 to get information about a user and in order to uniquely identify Instagram accounts even after name changes I store the "id" parameter that always stays the same.

Up to this point, it was possible to use another Instagram API Endpoint https://i.instagram.com/api/v1/users/{id}/info/ to find out the current username connected to a given account but it has been removed by Instagram a few days / weeks ago.

I'd highly appreciate if anyone knows another way of getting a username from a user id since I was not able to find one myself nor find a solution by someone else online.

Styria answered 12/10, 2019 at 12:56 Comment(0)
I
18

Ig has changed this endpoint behavior and it's been secured. However, the hint here is the error message

{
"message": "useragent mismatch",
"status": "fail"
}

Passing a valid useragent you can skip this new security-check. Let me share a piece of python code

import requests
def get_user_by_user_id(user_id):
    user = {}
    if user_id:
        base_url = "https://i.instagram.com/api/v1/users/{}/info/"
        #valid user-agent
        headers = {
            'user-agent':'Mozilla/5.0 (iPhone; CPU iPhone OS 12_3_1 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) Mobile/15E148 Instagram 105.0.0.11.118 (iPhone11,8; iOS 12_3_1; en_US; en-US; scale=2.00; 828x1792; 165586599)'
        }
        try:
            res       = requests.get(base_url.format(user_id),headers=headers)
            user_info = res.json()
            user      = user_info.get('user',{})
        except Exception as e:
            print("getting user failed, due to '{}'".format(e.message))
    return user

user_id=<your_user_id>
get_user_by_user_id(user_id) #ta-dah!

There should be some pluggin that allow you change headers in order to get results throught a browser request...

Imputation answered 23/10, 2019 at 6:7 Comment(5)
It returns a very low-detailed JSON in response (compared to old JSON responses from this endpoint)Bearable
@AmeerMousavi Use the newly-found username to get a detailed JSON response from this endpoint: instagram.com/<username>/?__a=1Lenhart
I don't know if this has changed at some point between when you answer was written and now, but as of now, you need to provide 4 additional parameters to make a query request -- besides the user agent. Here's a snippet that worked for me in Python: ``` headers={"user-agent": "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/102.0.5005.61 Safari/537.36", "x-asbd-id": "198387", "x-csrftoken": "VXLPx1sgRb8OCHg9c2NKXbfDndz913Yp", "x-ig-app-id": "936619743392459", "x-ig-www-claim": "0"}) ```Try
as far as I tested, they are not necessary....Imputation
The x-ig-app-id is only needed for desktop user agents, not mobile. I've never had to provide x-csrftoken or x-ig-www-claim. Note this user agent check is more than just a security check - it actually changes the JSON response, for example mobile user agents will only get smaller sized image URLs back.Awed
E
1

The below works
endpoint: "https://www.instagram.com/api/v1/users/web_profile_info/?username=${username}",
method: GET,
headers: {
'referer': 'https://www.instagram.com/${username}/',
'user-agent': 'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/127.0.0.0 Safari/537.36',
'x-ig-app-id': 'x-ig-app-id'
}

Ellon answered 25/8, 2024 at 14:56 Comment(1)
Nice catch bro!Sorely

© 2022 - 2025 — McMap. All rights reserved.