Twitter Query including a list_id with tweepy python

Viewed 49

I have created a Twitter List by the following lines:

    import tweepy
    
# name of the list
    name = "tweepy_list"
    
    client = tweepy.Client(consumer_key=consumer_key, consumer_secret=consumer_secret, access_token=access_token, access_token_secret=access_token_secret)
    l = client.create_list(name)
    id_l = l[0]['id']

Where id_l is the list id.

I have added users to the list by:

    client.add_list_member(id_l, user_id)

Now, I want to build a search query by this code and I want to include the list of users I previously created by:

    query = "{0} lang:es -is:retweet list:{1}:".format(topic, id_l)
    tweets = client.search_recent_tweets(query=query,
                                             tweet_fields = ["created_at", "text", "source"],
                                             user_fields = ["name", "username", "location", "verified", "description"],
                                             max_results = 10,
                                             expansions='author_id',
                                             user_auth=True
                                             )

But I am getting this error:

> BadRequest: 400 Bad Request
> There were errors processing your request: Reference to invalid operator 'list'. Operator is not available in current product or product packaging. Please refer to complete available operator list at https://developer.twitter.com/en/docs/twitter-api/enterprise/rules-and-filtering/operators-by-product

Can tweepy or another package support this kind of query from Twitter?

1 Answers

According to the Twitter documentation (here), the list operator is accessible only to the developpers with an elevated access. So the easiest solution would be to apply for a elevated access. You can also learn how to do that in the Twitter documentation (here).

On a side note: you are not using the value of the id_l variable in your query but litteraly the string 'id_l'. Don't forget to format it too (or to use f-strings).

Related