18

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])
ZdaR
  • 22,343
  • 7
  • 66
  • 87
user3643380
  • 195
  • 1
  • 1
  • 11
  • Did you authorise the app in your twitter account ? [help link][1] [1]: http://stackoverflow.com/questions/19095257/using-twython-to-send-a-tweet-twitter-api-error/19196583#19196583 – Tiket_dev Feb 09 '15 at 15:25
  • Unforunately, I still have the same error. – user3643380 Feb 09 '15 at 21:09
  • I also tried to check the sign in with twitter field, but with no use. Any help !!! – user3643380 Feb 09 '15 at 23:46
  • I guess the issue in in the date and time of your machine, kindly check if they are updated ? this is fairly common if your credentials are not correct or you are using an outdated version of tweepy – ZdaR Feb 10 '15 at 11:18
  • my version of tweepy is 2.7, actually I was retreiving data with no problem, but suddenly I faced this problem. Do not know the reason! – user3643380 Feb 10 '15 at 18:07
  • I have reinstalled tweepy version 3.2.0, but with no use, i got the same problem after renewing tweepy – user3643380 Feb 10 '15 at 20:02
  • 2
    Oh, I found the solution. At this link [link](http://stackoverflow.com/questions/26827790/401-error-while-using-tweepy) , it's all about the country,time settings in your computer.Thanks all – user3643380 Feb 10 '15 at 20:16
  • Does this answer your question? [401 error while using tweepy](https://stackoverflow.com/questions/26827790/401-error-while-using-tweepy) – user4157124 Nov 01 '22 at 00:17

7 Answers7

20

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

Prakhar Mishra
  • 211
  • 2
  • 3
  • 3
    I think it's actually 5 minutes, not 15 minutes. Also, this applies only to streaming requests. REST API requests will work fine even if the clock not in sync. – Jonas Dec 01 '15 at 20:52
6

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"]

techraf
  • 64,883
  • 27
  • 193
  • 198
smaug
  • 846
  • 10
  • 26
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'])
Ashish Patel
  • 59
  • 1
  • 9
2

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)
Spencer B.
  • 43
  • 5
0

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

user108569
  • 450
  • 5
  • 8
0

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.

0

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