question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Periodic http 401 errors from Twitter

See original GitHub issue

I am receiving HTTP 401 errors while running through a script I created that reads the Twitter stream and retweets things that match the parameters I have set. It occasionally fails during the retweet section with:

tweepy.error.TweepError: Twitter error response: status code = 401

I confirmed that the time is valid on the system and within the ~15 minute wiggle room. The code works the majority of the time, so I am not sure why it would throw the 401 status. Maybe a side question, but is there a more verbose error I can print? I know Twitter will return more information on why it threw 401 (such as unauthorized)

I snipped out some non-interesting parts, but here is the main part of the code:

def doRetweet(id_string):
    # authenticate against the Twitter API
    api = tweepy.API(auth)
    # actually do the retweet
    api.retweet(id_string)
    print('I did the retweet')
    print()
    return
class StdOutListener(tweepy.StreamListener):
    def on_data(self, data):

        # Twitter returns data in JSON format - we need to decode it first
        decoded = json.loads(data)

        # grab some data we use later
        tweet_id = decoded['id_str']

        doRetweet(tweet_id)

        return True

    def on_error(self, status):
        print('The below status code was returned from Twitter')
        print(status)
        if status_code == 420:
            #returning False in on_data disconnects the stream
            return False

    def on_exception(self, exception):
        raise exception
        return False

try:
    if __name__ == '__main__':
        l = StdOutListener()
        auth = tweepy.OAuthHandler(consumer_key, consumer_secret)
        auth.set_access_token(access_token, access_token_secret)

        # Authenticate with the streaming API and filter what we initially receive
        # spaces are AND operators, while a comma indicates OR. More info here: https://dev.twitter.com/streaming/overview/request-parameters
        stream = tweepy.Stream(auth, l)
        stream.filter(track=['#blah'], languages=['en'])
except KeyboardInterrupt:
    sys.exit()

Issue Analytics

  • State:open
  • Created 8 years ago
  • Comments:6 (5 by maintainers)

github_iconTop GitHub Comments

1reaction
coucosethcommented, Jan 11, 2022

was this fixed because i have the same issue

0reactions
geransmithcommented, Mar 31, 2015

No luck with the try/except section. “on_exception” in streaming.py is catching the exception before I can print out the response. At a minimum I am getting the 401 status code.

Read more comments on GitHub >

github_iconTop Results From Across the Web

401 error, Timestamp out of bounds - Twitter Developers
Failed to get mentions: 401:Authentication credentials ( Authentication overview | Docs | Twitter Developer Platform) were missing or incorrect. Ensure that you ...
Read more >
Need help finding why code is giving me a HTTP 401 error for ...
having trouble seeing as to why my code is giving me a Stream encountered HTTP error: 401 for my twitter bot. If anyone...
Read more >
What is a 401 Error and How Do You Fix It? - Elegant Themes
An HTTP error 401 means there's a problem authenticating your browser credentials. In this article, we'll break down what that means and what ......
Read more >
Resolve errors | Gmail - Google Developers
Resolve a 401 error: Invalid credentials ... To fix this error, refresh the access token using the long-lived refresh token. If you are...
Read more >
401 Error Code using API Management - Boomi Community
A 401 is an HTTP Response code indicating that the client is not authenticated to the given server it sends a request to....
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found