skip to Main Content

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


  1. 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

    client.search_recent_tweets(query=query, max_results=10,
    user_auth=True)
    

    At least, I can solved it in this way.

    Login or Signup to reply.
  2. I had the same problem. Make sure to pass the bearer_token to the client

    See below:

    client = tweepy.Client(consumer_key=api_key,
                        consumer_secret=api_key_secret,
                        access_token=access_token,
                        access_token_secret=access_secret_key,
                        bearer_token=bearer_token)
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search