简体   繁体   中英

Get username by id in Twitter Api

How can I get the user name by userid via a request to the Twitter Api?

Some other methods, like followers/ids , give us as response an array of IDs, from which I am not sure how I can get their usernames.

The response of the method " followers/ids " looks something like this:

{
"ids": [782235942383972400, 16664925, 839240398010781700, 39606379, 19713521, 
...
...
...
...
...
899462802, 198446818],
"next_cursor": 0,
"next_cursor_str": "0",
"previous_cursor": 0,
"previous_cursor_str": "0"
}

How I can get the username from these ids? I want something like what this webpage does.

Use GET https://api.twitter.com/1.1/users/show.json?user_id=ID and see the screen_name property of the returned JSON object.

Reference: https://api.twitter.com/1.1/users/show.json

Since you're getting multiple Twitter users from followers/ids , you'll probably want to look up the corresponding screen names in bulk. The users/show API endpoint mentioned in the other answer only handles one user. For up to 100 users, use users/lookup .

This has changed in the Twitter 2 api. To get the username of a single user:

https://api.twitter.com/2/users/123456

For multiple users (up to 100), use:

https://api.twitter.com/2/users?ids=123456,456789"

Click here for more info

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM