'401 Error when retrieving Twitter data using Tweepy

I am trying to retrieve Twitter data using Tweepy, using that below code, but I'm returning 401 error, and I regenerate the access and secret tokens, and the same error appeared.

#imports
from tweepy import Stream
from tweepy import OAuthHandler
from tweepy.streaming import StreamListener

#setting up the keys
consumer_key = 'xxxxxxx'
consumer_secret = 'xxxxxxxx' 
access_token = 'xxxxxxxxxx'
access_secret = 'xxxxxxxxxxxxx'

class TweetListener(StreamListener):
# A listener handles tweets are the received from the stream.
#This is a basic listener that just prints received tweets to standard output

   def on_data(self, data):
       print (data)
       return True

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



#printing all the tweets to the standard output
auth = OAuthHandler(consumer_key, consumer_secret)
auth.set_access_token(access_token, access_secret)



stream = Stream(auth, TweetListener())

t = u"#سوريا"
stream.filter(track=[t])


Solution 1:[1]

Just reset your system's clock.

If an API request to authenticate comes from a server that claims it is a time that is outside of 15 minutes of Twitter time, it will fail with a 401 error.

ThankYou

Solution 2:[2]

You might just have made a mistake in copying the Access Token from the apps.twitter.com page.

You need to copy the entire thing that's given as Access Token, not just the string after the -.

For example, copy and paste the entire string like 74376347-jkghdui456hjkbjhgbm45gj, not just jkghdui456hjkbjhgbm45gj.

[Note the above string is just something I typed randomly for demonstration purpose. Your actual Access token will also look like this though, i.e, "a string of number-an alphanumeric string"]

Solution 3:[3]

you just have to show your keys into the double quote and you don't have to define your keys in last twitter authentication.

#Import the necessary methods from tweepy library
from tweepy.streaming import StreamListener
from tweepy import OAuthHandler
from tweepy import Stream

#Variables that contains the user credentials to access Twitter API
access_token = 'X3YIzD'
access_token_secret = 'PiwPirr'

consumer_key = 'ekaOmyGn'
consumer_secret = 'RkFXRIOf83r'


#This is a basic listener that just prints received tweets to stdout.
class StdOutListener(StreamListener):

def on_data(self, data):
    print data
    return True

def on_error(self, status):
    print status


if __name__ == '__main__':

#This handles Twitter authetification and the connection to Twitter 
Streaming API
l = StdOutListener()
auth = OAuthHandler(consumer_key, consumer_secret)
auth.set_access_token(access_token, access_token_secret)
stream = Stream(auth, l)

#This line filter Twitter Streams to capture data by the keywords: 'python', 
'javascript', 'ruby'
stream.filter(track=['python', 'javascript', 'ruby'])

Solution 4:[4]

I had the same issue - nothing here fixed it. The trick for me was that Streaming tweets with Tweepy apparently requires 1A authentication, not 2A (see - https://github.com/tweepy/tweepy/issues/1346). This means you need to use an access token as well as the consumer tokens in the authentication object.

import tweepy

# user credentials
access_token = '...'
access_token_secret = '...'

consumer_key = '...'
consumer_secret = '...'

auth = tweepy.OAuthHandler(consumer_key, consumer_secret)

# this is the main difference
auth.set_access_token(access_token, access_token_secret)

stream = tweepy.Stream(auth, tweepy.StreamListener)

Solution 5:[5]

In my case the error occurred because I was using AppAuthHandler rather than OAuthHandler. Switching to OAuthHandler resolved the issue.

Solution 6:[6]

In my case, I had this problem but it did not have to do with time.

My app had a "read only" permission. I had to change it to a "read and write" permission for the error to cease.

You can do this by going to "user authentication" in the app settings page.

Solution 7:[7]

After changing your read only permission, you have to regenerate your access token, then put it into your code. Thanks for the help!

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Prakhar Mishra
Solution 2 techraf
Solution 3
Solution 4 Spencer B.
Solution 5 user108569
Solution 6 Joshua Okonkwo
Solution 7 Typowy Jnn