New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Periodic http 401 errors from Twitter #582

Open
tehspaceg opened this Issue Mar 27, 2015 · 5 comments

Comments

Projects
None yet
2 participants
@tehspaceg
Contributor

tehspaceg commented Mar 27, 2015

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()
@tehspaceg

This comment has been minimized.

Contributor

tehspaceg commented Mar 27, 2015

Just food for thought, could it be because I am being login rate limited when doing the retweet? I am doing the login stuff in the retweet function, so perhaps I am being rate limited if I get a few retweets in a short window?

Could I just authenticate once in the "f name == 'main':" section of my code?

@tehspaceg

This comment has been minimized.

Contributor

tehspaceg commented Mar 28, 2015

Moving the api = tweepy.API(auth) section down to the main section did not resolve the 401 errors. Trying out a few things. Any input would be helpful, I am mostly just throwing darts.

@yprez

This comment has been minimized.

Contributor

yprez commented Mar 28, 2015

Tweepy exceptions have a response attribute that holds the original response from twitter. Maybe it'll help.
If you do something like:

try:
    retweet()
except Exception as e:
    if e.response:
        print(e.response.content)
    raise

Twitter usually tells you if you're being rate limited.

I also noticed that you're not setting the access token in doRetweet() (auth.set_access_token()), maybe that's the reason?

@tehspaceg

This comment has been minimized.

Contributor

tehspaceg commented Mar 28, 2015

I'll put the exception bit into my code to see if it gives more info instead of just the 401. I added some retry bits into the auth, so if it error out again I will add the exception print.

I was under the impression that I only needed one access token and it didn't time out, I have the auth.set_access_token() running before we hop into the streaming section.

@tehspaceg

This comment has been minimized.

Contributor

tehspaceg commented 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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment