2012-09-25 171 views
5

我試圖訪問,同時使用不當,其Tweepy我以前工作Twitter的數據流。現在我明白了Tweepy是如何使用的,我寫了下面的Stream.py模塊。當我運行它時,我收到錯誤代碼401,它告訴我我的認證已被拒絕。但是,我早先使用相同的消費者令牌和祕密工作。有任何想法嗎?與Tweepy Twitter的流API拒絕的OAuth

from tweepy.streaming import StreamListener 
from tweepy import OAuthHandler 
from tweepy import Stream 
from tweepy import TweepError 
from tweepy import error 

#Removed. I have real keys and tokens 
consumer_key = "***" 
consumer_secret = "***" 
access_token="***" 
access_token_secret="***" 

class CustomListener(StreamListener): 
    """ A listener handles tweets are the received from the stream. 
     This is a basic listener that just prints received tweets to stdout.""" 

    def on_status(self, status): 
     # Do things with the post received. Post is the status object. 
     print status.text 
     return True 

    def on_error(self, status_code): 
     # If error thrown during streaming. 
     # Check here for meaning: 
     # https://dev.twitter.com/docs/error-codes-responses 
     print "ERROR: ",; print status_code 
     return True 

    def on_timeout(self): 
     # If no post received for too long 
     return True 

    def on_limit(self, track): 
     # If too many posts match our filter criteria and only a subset is 
     # sent to us 
     return True 

    def filter(self, track_list): 
     while True: 
      try: 
       self.stream.filter(track=track_list) 
      except error.TweepError as e: 
       raise TweepError(e) 

    def go(self): 
     listener = CustomListener() 
     auth = OAuthHandler(consumer_key, consumer_secret) 
     self.stream = Stream(auth,listener,timeout=3600) 
     listener.filter(['LOL']) 

if __name__ == '__main__': 
    go(CustomListener) 

回答

3

對於任何人誰碰巧有同樣的問題,我應該加入這一行AUTH被初始化後:

auth.set_access_token(access_token, access_token_secret)