【发布时间】:2018-06-23 12:53:05
【问题描述】:
我正在尝试使用 tweepy API 流,但我不断收到错误 401。我在网上寻找线索,我发现的每个线程都说是因为时区设置。这可能不是我的情况。
在我的场景中,我可以与 Twitter API (verify_credentials) 通信,但是当我创建一个 Stream 对象并使用过滤方法触发它时,我不断收到错误 401。这是我的侦听器 类:
class MyStreamListener(tweepy.StreamListener):
def on_status(self, status):
print(status.text)
def on_error(self, err):
print(err)
这是我正在测试的代码:
auth = tweepy.OAuthHandler(consumer_key, consumer_secret)
auth.set_access_token(access_token, access_token_secret)
api = tweepy.API(auth)
listener = MyStreamListener()
myStream = tweepy.Stream(auth=api.auth, listener=listener)
401
api.verify_credentials()
User(_api=<tweepy.api.API object at 0x02E30DF0>, _json={'id':...})
如您所见, api.verify_credentials() 成功。但是在创建流时出现错误 401(根据 twitter 的文档,未经授权和其他场景)。
tweepy 模型的更多调试输出:
2018-01-14 20:51:43,025 requests_oauthlib.oauth1_auth - DEBUG: Updated url: https://stream.twitter.com/1.1/statuses/filter.json?delimited=length
2018-01-14 20:51:43,025 requests_oauthlib.oauth1_auth - DEBUG: Updated headers: {'Content-Type': 'application/x-www-form-urlencoded', 'Content-Length': '17', 'Authorization': 'OAuth oauth_nonce="xxx", oauth_timestamp="1515955903", oauth_version="1.0", oauth_signature_method="HMAC-SHA1", oauth_consumer_key="xxx", oauth_token="xxx-xxx", oauth_signature="xxx"'}
2018-01-14 20:51:43,025 requests_oauthlib.oauth1_auth - DEBUG: Updated body: 'track=python%2Clive'
2018-01-14 20:51:43,026 urllib3.connectionpool - DEBUG: Starting new HTTPS connection (1): stream.twitter.com
2018-01-14 20:51:44,675 urllib3.connectionpool - DEBUG: https://stream.twitter.com:443 "POST /1.1/statuses/filter.json?delimited=length HTTP/1.1" 401 283
2018-01-14 20:51:44,676 api.twitter - ERROR: error occurred while listening for new statuses: err=401
到目前为止我已经尝试过:
- 允许完全访问(读取、写入和直接消息)
- 生成新密钥
- 手动设置我的 Windows 10 时区
- 自动配置时区
- 已验证我在 Twitter 上的时区是否同步
我正在使用 Python 3.6.4 和 Tweepy 3.5
这是非常基本的场景。任何建议都非常受欢迎。
【问题讨论】:
标签: python twitter streaming tweepy