python - 带有 Tweepy 的 Twitter Streaming API 拒绝 oauth

标签 python twitter tweepy twitter-streaming-api

我正在尝试访问我之前在不正确地使用 Tweepy 时工作的 Twitter 流。现在我了解了 Tweepy 的用途,我编写了以下 Stream.py 模块。当我运行它时,我收到错误代码 401,它告诉我我的身份验证已被拒绝。但是我早些时候使用相同的消费者 token 和 secret 让它工作。有什么想法吗?

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)

最佳答案

对于碰巧遇到同样问题的人,我应该在初始化身份验证后添加这一行:

auth.set_access_token(access_token, access_token_secret)

关于python - 带有 Tweepy 的 Twitter Streaming API 拒绝 oauth,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12592640/

相关文章:

python - 删除qt中的子布局?

python - 检查跨二维数组的滑动窗口中的所有元素是否为 True - Python

ios - TWTRLogInButton(推特登录按钮)设置标题导致文字重叠

ios - 推特登录成功后从推特 native 应用程序重定向到 iOS 应用程序

python - Tweepy API : how to get a user's id from a status SearchResult object?

python - 如何从Python3中的变量/文本文件中删除b和\n? (类型错误)

python - 将多个 pandas 数据帧中的所有列连接到一个包含数据和列名称的数据帧中

带有 Meteor 的 Twitter API 1.1 Oauth

python - 在 Tweepy 中标记时获取推文

python - Tweepy Streaming - 停止收集 x 数量的推文