Skip to content

Method: User lookup

Matthew R. DeVerna edited this page Aug 1, 2021 · 3 revisions

User lookup endpoints return the account information. It supports two different types of queries:

  1. Query using user ID numbers
  2. Query using the account's username

Note: A user can change their username but they can't change their id number. It is therefore recommended that you gather data using id numbers because they are more reliable.

We can query Twitter with up to 100 user IDs or usernames per query. Here's what it looks like to pull two user IDs with the .user_lookup_ids() method, which requires we pass it a list (or tuple) of account user IDs.

Pull account information with user_id numbers - user_lookup_ids()

import osometweet

# Initialize the OSoMeTweet object
bearer_token = "YOUR_TWITTER_BEARER_TOKEN"
oauth2 = osometweet.OAuth2(bearer_token=bearer_token)
ot = osometweet.OsomeTweet(oauth2)

# Set some test IDs (these are Twitter's own accounts)
ids2find = ["2244994945", "6253282"]

# Call the function with these ids
response = ot.user_lookup_ids(ids2find)
print(response["data"])

which returns a list of dictionaries, where each dictionary contains the requested information for an individual user.

[
    {'id': '2244994945', 'name': 'Twitter Dev', 'username': 'TwitterDev'},
    {'id': '6253282', 'name': 'Twitter API', 'username': 'TwitterAPI'}
]

Pull account information with usernames - user_lookup_usernames()

The process for pulling account information using an account's user names is exactly the same as using user_ids except we now use the user_lookup_usernames() method and provide usernames. So, after initializing the OsomeTweet with your Twitter tokens/keys, you can do the following:

# Set some test IDs (these are Twitter's own accounts)
usernames2find = ["TwitterDev", "TwitterAPI"]

# Call the function with these ids
response = ot.user_lookup_usernames(usernames2find)
print(response["data"])

which returns the same results as before.


Fields & expansions

V2 API, by default, only returns limited information. To fetch more, you will need to specify the fields and expansions parameters in the requests. OSoMeTweet contains several classes to handle them. See Method: Specifying fields and expansions for examples of how to work with them.