Twitter Streaming API使用Tweepy时拒绝OAuth验证

5

我曾经使用不正确的Tweepy方式访问Twitter流,现在我理解了Tweepy应该如何使用,编写了以下Stream.py模块。当我运行它时,出现错误代码401,告诉我我的授权已被拒绝。但是我之前使用相同的消费者令牌和密钥可以工作。有任何想法吗?

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)
1个回答

3
对于有相同问题的人,我应该在初始化认证后添加这行代码:
auth.set_access_token(access_token, access_token_secret)

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接