Lord
Lord

Reputation: 153

How to get instagram username from username ID without API in Python?

I'm trying to find a way to get instagram username from the username ID in Python. What I found so far is this:

https://api.instagram.com/v1/users/[USER-ID]?access_token=[YOUR-ACCESS-TOKEN]

I have a token number, but it only works for users added to sandbox. For other users I receive the following error:

{"meta": {"code": 400, "error_type": "APINotFoundError", "error_message": "this user does not exist"}}

I'm searching a solution for hours with no luck so far. Is there a way to arrive to the same result without using a token?

Thanks a lot!

Upvotes: 3

Views: 9705

Answers (3)

aandergr
aandergr

Reputation: 637

Point your browser to https://i.instagram.com/api/v1/users/USERID/info/ (replace USERID by the user id). It returns a JSON in which the field user.username is that user's username. With curl and jq installed, this shell oneliner will directly return the username:

curl -s https://i.instagram.com/api/v1/users/USERID/info/ | jq -r .user.username

Alternatively, the Instaloader package provides a convenient way to obtain a username given its ID, without using the official Instagram API.

import instaloader

L = instaloader.Instaloader()
profile = instaloader.Profile.from_id(L.context, ID)
print(profile.username)

It works by querying one of the profile's posts (for which only the profile's ID is required), obtaining that post's metadata (instagram.com/p/SHORTCODE) and then returning the owner.username value of that post.

Upvotes: 5

Zohab Ali
Zohab Ali

Reputation: 9584

In my case I am getting data from tagFeed so I have picture code. If you have picture code of any of user's post then you can get username like this

https://www.instagram.com/p/picCODE/?__a=1

example: https://www.instagram.com/p/BaGce-NlMg7/?__a=1

it will return a JSON with the owner - username

Upvotes: 6

tompec
tompec

Reputation: 1230

No, unfortunately you can't. You need a token with public_content permissions, and for that, you need to have a live app (not in sandbox).

Upvotes: 0

Related Questions