401 error while using tweepy
Asked Answered
S

1

9

I am running the below program.But am getting the below error Message. 401 **** 401 **** (keeps on repeating)

The code(got from some forum) basically tries to connect to Twitter and fetch tweets. When its ran on ubuntu terminal the 401 error message appear.

import sys
import json
import pymongo
import tweepy
consumer_key="XX" ##all the keys and codes have to be strings
consumer_secret="XX"
access_token = "XX"
access_token_secret = "XX"
# This is the listener, resposible for receiving data

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)



        # Also, we convert UTF-8 to ASCII ignoring all bad characters sent by users

        print '@%s: %s' % (decoded['user']['screen_name'], decoded['text'].encode('ascii', 'ignore'))

        print ''

        return True



    def on_error(self, status):

        print status



if __name__ == '__main__':

    l = StdOutListener()

    auth = tweepy.OAuthHandler(consumer_key, consumer_secret)

    auth.set_access_token(access_token, access_token_secret)



    print "Showing all new tweets for #programming:"



    # There are different kinds of streams: public stream, user stream, multi-user streams

    # In this example follow #programming tag

    # For more details refer to https://dev.twitter.com/docs/streaming-apis

    stream = tweepy.Stream(auth, l)

    stream.filter(track=['programming'])
Sandy answered 9/11, 2014 at 11:44 Comment(2)
You're using your correct consumer/access tokens right? Not just the ones from "some forum" or "XX"?Elecampane
Am using the correct ones which i got generated from twitter developer siteSandy
S
8

This is how it works..!!!

  • Twitter keeps track of the current time.
  • 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.

Just reset your system's clock according to world clock or let it govern by internet your problem will be solved.

Good luck..!!!

Sucrose answered 18/12, 2014 at 14:7 Comment(3)
Hellow how can i do that on Ubuntu 14.10Barghest
Go to Settings and Set your clock according to the global time zone.Sucrose
IMP: Don't forget to change your timezone in your twitter account and also verify your email! One of these worked for meLariat

© 2022 - 2024 — McMap. All rights reserved.