401 Error when retrieving Twitter data using Tweepy
Asked Answered
Y

7

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])
Yale answered 9/2, 2015 at 14:59 Comment(8)
Did you authorise the app in your twitter account ? [help link][1] [1]: #19095757Gynaeco
Unforunately, I still have the same error.Yale
I also tried to check the sign in with twitter field, but with no use. Any help !!!Yale
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 tweepyUnrestrained
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!Yale
I have reinstalled tweepy version 3.2.0, but with no use, i got the same problem after renewing tweepyYale
Oh, I found the solution. At this link link , it's all about the country,time settings in your computer.Thanks allYale
Does this answer your question? 401 error while using tweepyErudite
L
19

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

Lesialesion answered 30/11, 2015 at 11:21 Comment(2)
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.Wife
What about different timezones?Finery
I
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"]

Interval answered 10/4, 2016 at 11:41 Comment(0)
G
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'])
Groat answered 5/10, 2017 at 14:23 Comment(0)
P
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)
Pretypify answered 14/2, 2021 at 16:3 Comment(1)
Hi i am getting 401 unauthorisedOrganic
L
0

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

Leboff answered 25/1, 2018 at 18:33 Comment(0)
S
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.

Savory answered 2/3, 2022 at 20:13 Comment(1)
could you please add some screenshots to illustrate these instructions?Lw
H
0

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

Halfdan answered 1/5, 2022 at 10:5 Comment(0)

© 2022 - 2025 — McMap. All rights reserved.