Chen A.
Chen A.

Reputation: 11280

Tweepy streaming returns error 401

I'm trying to use tweepy API streaming, but I keep getting error 401. I've looked online for clues, and every thread I found says its because of timezone settings. That's probably not my case.

In my scenario, I can communicate with the Twitter API (verify_credentials), but when I'm creating a Stream object and trigger it with filter method, I keep getting error 401. This is my listener class:

class MyStreamListener(tweepy.StreamListener):
    def on_status(self, status):
        print(status.text)

    def on_error(self, err):
        print(err)

And this is the code I'm testing:

auth = tweepy.OAuthHandler(consumer_key, consumer_secret)
auth.set_access_token(access_token, access_token_secret)
api = tweepy.API(auth)
listener = MyStreamListener()
myStream = tweepy.Stream(auth=api.auth, listener=listener)
401

api.verify_credentials()
User(_api=<tweepy.api.API object at 0x02E30DF0>, _json={'id':...})

As you see, api.verify_credentials() succeeds. But when creating stream I get error 401 (unauthorized and additional scenarios according to twitter's docs).

Some more debugging output from the tweepy model:

2018-01-14 20:51:43,025 requests_oauthlib.oauth1_auth -  DEBUG: Updated url: https://stream.twitter.com/1.1/statuses/filter.json?delimited=length
2018-01-14 20:51:43,025 requests_oauthlib.oauth1_auth -  DEBUG: Updated headers: {'Content-Type': 'application/x-www-form-urlencoded', 'Content-Length': '17', 'Authorization': 'OAuth oauth_nonce="xxx", oauth_timestamp="1515955903", oauth_version="1.0", oauth_signature_method="HMAC-SHA1", oauth_consumer_key="xxx", oauth_token="xxx-xxx", oauth_signature="xxx"'}
2018-01-14 20:51:43,025 requests_oauthlib.oauth1_auth -  DEBUG: Updated body: 'track=python%2Clive'
2018-01-14 20:51:43,026 urllib3.connectionpool -  DEBUG: Starting new HTTPS connection (1): stream.twitter.com
2018-01-14 20:51:44,675 urllib3.connectionpool -  DEBUG: https://stream.twitter.com:443 "POST /1.1/statuses/filter.json?delimited=length HTTP/1.1" 401 283
2018-01-14 20:51:44,676 api.twitter -  ERROR: error occurred while listening for new statuses: err=401

So far I've tried:

I'm using Python 3.6.4 and Tweepy 3.5

This is very basic scenario. Any suggestions are more than welcome.

Upvotes: 0

Views: 1008

Answers (1)

Chen A.
Chen A.

Reputation: 11280

I'm ashamed, but this is due to time zone issue. My Windows was configured with automatic time, but when I looked at Date & Time -> Internet Time, I noticed there is a problem syncing with the windows ntp server.

I configured something else, pressed update now, got it working and my code works now. So, if you encounter this as well, VERIFY windows ntp server is accessible and working for you.

Upvotes: 1

Related Questions