Receiving 401 error in Twython when trying to get tweets

75 Views Asked by At

I'm trying to get the most recent 100 tweets from certain Twitter users on my dev account. I have verified that the credentials work because I can use the API for other tasks. However, the code below produces the subsequent error:

from twython import Twython

with open("twitter_credentials.json", "r") as file:
    creds = json.load(file)
    
python_tweets = Twython(creds['CONSUMER_KEY'], creds['CONSUMER_SECRET']) #object that is our credentials

query1 = {'q' : 'from:@Mike_Pence',
          'count': 100,
          'exclude' : 'retweets'}

pence = python_tweets.get_user_timeline(**query1, tweet_mode='extended') #extended gives full text of tweets

for tweets in pence['statuses']:
    print(tweets['full_text'] + '\n')

The error I receive is:

TwythonAuthError: Twitter API returned a 401 (Unauthorized), An error occurred processing your request.

I'd be grateful for any help please.

1

There are 1 best solutions below

1
finally On

Seems that you're using the Twitter API too often. Due to security reasons, Twitter has rate limiting on the usage of their Twitter API from an arbitrary app. Twitter says in their post how their Twitter API rate-limiting works. You can make a maximum of 15 GET requests under 15 minutes from a specific set of authorization keys of that particular app. After 15 minutes, you are free to make the next 15 requests available.

also you can try to recreate your auth keys