How come I can create a tweet and like a tweet with no 401 errors? When I try to search for tweets Im getting a 401?
Here’s my create_tweet.py file (works fine)
import tweepy
import config
# calling a client
client = tweepy.Client(
consumer_key=config.consumer_key,
consumer_secret=config.consumer_secret,
access_token=config.access_token,
access_token_secret=config.access_token_secret
)
response = client.create_tweet(text='twitter api testoooor')
print(response)
But my search.py file keeps getting a 401 error:
import tweepy
import config
# calling a client
client = tweepy.Client(
consumer_key=config.consumer_key,
consumer_secret=config.consumer_secret,
access_token=config.access_token,
access_token_secret=config.access_token_secret
)
response = client.search_recent_tweets(query=query, max_results=10)
for tweet in response.data:
print(tweet.id)
Really weird that the create_tweet.py isn’t getting a 401 error but my search.py file is.
2
Answers
I face the same problem as you faced.
Maybe this problem has been solved, just to be sure, I describe the solution.
I solved this problem by looking at the answers below.
Tweepy: tweepy.errors.Unauthorized: 401 Authorization Required
please add "user_auth=True" like below
At least, I can solved it in this way.
I had the same problem. Make sure to pass the
bearer_token
to the clientSee below: