Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

User ID to Username tweepy

Can someone please tell me how to get one username from one id on tweepy? I have looked everywhere and can't find anything.

like image 294
Amro elaswar Avatar asked Mar 24 '15 01:03

Amro elaswar


People also ask

How can I get twitter user ID?

Go to www.twitter.com and sign in with your account. Then click on your profile picture at the top bar and select Settings and privacy from the drop-down menu. Open 'Your Twitter data' Tab.

How do I find my Tweepy status ID?

Identifying the ID in the GUI : In order to get the ID of a status, we have to do the following : Identify the status ID of the status from the GUI. Get the Status object of the status using the get_status() method with the status ID. From this object, fetch the id attribute present in it.

Does Tweepy work with v2?

If you want to retweet a Tweet with Tweepy using the Twitter API v2, you will need to make sure that you have your consumer key and consumer secret, along with your access token and access token secret, that are created with Read and Write permissions (similar to the previous example).


2 Answers

If you just have a user_id value you need to call the twitter API with the get_user(user_id) method. The returned User object will contain the username under screen_name.

# steps not shown where you set up api
u = api.get_user(783214)
print u.screen_name

If you already have the User object from another API call just look for the screen_name.

like image 97
Juan E. Avatar answered Oct 23 '22 16:10

Juan E.


You can use this code to get user screen name or user id

To get user screen name from user id

In [36]: import tweepy
In [37]: consumer_key = Your_consumerkey
In [38]: consumer_secret = Your_consuersecret
In [39]: access_token = Your_access_token
In [40]: access_token_secret = Your_access_token_secret
In [41]: auth = tweepy.OAuthHandler(consumer_key, consumer_secret)
In [42]: auth.set_access_token(access_token, access_token_secret)
In [43]: api = tweepy.API(auth)
In [48]: user = api.get_user(1088398616)                                                           

In [49]: user.screen_name
Out[49]: u'saimadhup'

To get user id from user screen name

In [46]: user = api.get_user(screen_name = 'saimadhup')
In [47]: user.id
Out[47]: 1088398616
like image 11
saimadhu.polamuri Avatar answered Oct 23 '22 17:10

saimadhu.polamuri