1

I am trying to write a function that can succesfully create a List of all (or some) accounts who have liked a Tweet. To achieve this I have used Tweepys get_liking_users function. I have previously succesfully requested the Tweet Infos of some Tweets and am now using the Tweet ID of a Tweet I know for certain has been both liked and rewteeted (I checked it on Twitter).

This is my code:


def findLikers():
    client = tweepy.Client(bearer_token='XXX')

    id = 'XXX'

    users = client.get_liking_users(id=id, max_results=10)

    print(type(users))
    print(users)

However, when running this I get an empty response as exemplified by my print statements. This is the Output:

<class 'tweepy.client.Response'>
Response(data=None, includes={}, errors=[], meta={'result_count': 0})

I'm pretty certain that the Tweet ID is correct, I looked it up on Twitter to ensure it is the tweet I mean (and actually has likes and retweets) and ran a seperate request to return the Tweet Text for the ID, which worked. However when running get_liking_users() or get_retweers() it consistently returns data=None, even though as I said, according to the UI there are both likes and retweets.

I tried to follow the tutorial here: https://dev.to/twitterdev/a-comprehensive-guide-for-using-the-twitter-api-v2-using-tweepy-in-python-15d9; and also saw this: Tweepy : How to get user id who liked(fav) the specified tweet, however I was unable to fix the problem.

(As the functions ittself are pretty new, I think Twitter introduced an endpoint for this in May 2021, I could not find a lot of material on them except the Stackoverflow post above)

Can anyone see what I'm doing wrong?

(If there's any more Information I can provide, I'd be happy to, this is my first time asking something here)

I tried running the tweepy get_liking_users function and also tried the same steps with the get_retweeters function. I was hoping, the request would return the User ID's of all Twitter accounts that had liked/retweeted the Tweet. However The data is always None. I tried using some other ID's of some other Tweets, however the result stayed the same.

marley
  • 13
  • 3

1 Answers1

0

The meta resoult_count 0 means, your tested tweet ID no liked people.

I demo one of tweet from Nike with your looking users who liked Tweet by API and Tweepty.

Nike -> user id

1582388225049780230 -> tweet id

https://twitter.com/Nike/status/1582388225049780230

enter image description here

API

#1 liked user for tweet

https://api.twitter.com/2/tweets/1582388225049780230/liking_users

enter image description here

enter image description here

https://api.twitter.com/2/tweets/1582388225049780230/?tweet.fields=public_metrics&expansions=attachments.media_keys&media.fields=public_metrics

We can get the numbers for retweet number and liked number enter image description here

Get Tweet liked user list by tweepy

import tweepy

bearer_token ="*************************"

client = tweepy.Client(bearer_token=bearer_token)

tweet_id="1582388225049780230"
users = client.get_liking_users(id=tweet_id, max_results=5)

print(type(users))          # <class 'tweepy.client.Response'>
print(type(users.data))     # <class 'list'>
print(type(users.data[0]))  # <class 'tweepy.user.User'>

for user in users.data:
    print(user.name, " -> ",user.id,",",user.username)

    # jose rojas say  ->  1610877195710304256 , rojassay1
    # Ochy Ras  ->  1400520916795281409 , GirimbaMweusi
    # Tapiwanashe Kandoje  ->  1497970355872452612 , TapiwanasheKan

You can see matched liked users (last 3 people) as previous screen image.

The returned users type is tweepy.client.Response You can see tweepy's API

https://docs.tweepy.org/en/stable/client.html#likes

enter image description here

If click last red box, will go API likes

https://developer.twitter.com/en/docs/twitter-api/tweets/likes/api-reference/get-tweets-id-liking_users

enter image description here

if scroll down, you can see example of response

the data and it's child array item are id, name and username That WHY my for loop print those variables.

enter image description here

Full code in here get all of re tweeter user list

Get Re Tweet User list by tweepy

Same idea how to get type and how to print it as same as get liked user list.

import tweepy

bearer_token ="*************************"

client = tweepy.Client(bearer_token=bearer_token)

tweet_id="1582388225049780230"
retweeters = client.get_retweeters(id=tweet_id, max_results=3)

print(type(retweeters))          # <class 'tweepy.client.Response'>
print(type(retweeters.data))     # <class 'list'>
print(type(retweeters.data[0]))  # <class 'tweepy.user.User'>

for retweeter in retweeters.data:
    print(retweeter.name, " -> ",retweeter.id,",",retweeter.username)

    # valmig  ->  1594136795905593344 , AngelVa00615402
    # Wyatt Jones  ->  764734669434871808 , TheGhostZeus
    # Prime Projects  ->  1603887705242435584 , PrimeProjects4

It will matched retweet user's list from UI. enter image description here

Bench Vue
  • 5,257
  • 2
  • 10
  • 14
  • Hey there, thank you so much for your answer! After reading through your answer and code, I looked at mine again and tried it with the ID from your post, and it worked! I just had to slightly edit the print to use `user.name.encode("utf-8")` because without that I kept getting the error `UnicodeEncodeError: 'charmap' codec can't encode characters in position 11-12: character maps to ` – marley Jan 08 '23 at 14:48
  • I also tried some of the Tweet ID's I had before with the reworked code and realized why they returned None: They did have likes and retweets but the posts themselves were just retweets, I think that's why I got None as a result so often. Thank you so much for your help! – marley Jan 08 '23 at 14:53
  • you are welcome, I am happy to hear you got it. – Bench Vue Jan 09 '23 at 14:44