Tweepy Streaming Return返回401授权



我正在尝试连接到Twitter流API,以实时获取推文。直到5-6岁时,这件代码一直在运行。突然,我一直开始收到401。奇怪的是,这两者都在我的本地机器和云中的生产服务器上发生,所以我认为这与网络无关。

这是代码:

l = StdOutListener()
auth = OAuthHandler(consumer_key, consumer_secret)
auth.set_access_token(access_token, access_token_secret)
auth.callback = 'https://localhost' #no effect commented/uncommented
stream = Stream(auth, l)
stream.filter(track=['twitter'], languages=["es"])

到目前为止,我已经尝试了以下内容:

  1. 生成新的消费者密钥,消费者秘密,访问令牌和访问令牌秘密。我使用了以前工作的4组键。
  2. 从apps.twitter.com创建了一个新的新应用。填写回调URL,因为一些用户报告说401"必需授权"与Twitter应用程序页面中缺少的回调URL字段有关。
  3. 在我的本地和生产服务器上,将我的时钟与NTP服务器同步。我的时钟没有关闭。
  4. 我的应用程序具有正确的特权,我在配置后收到了访问令牌和秘密。

这是请求:

'Content-Length': '22'
'Content-Type': 'application/x-www-form-urlencoded'
'Authorization': 'OAuth oauth_nonce="161484371946745487981492681844"
    oauth_timestamp="1492681844"
    oauth_version="1.0"
    oauth_signature_method="HMAC-SHA1"
    oauth_consumer_key="<oauth_consumer_key>"
    oauth_token="<oauth_token>"
    oauth_signature="tphQqFWaBvEo2byjZ%2BRqNAM30I0%3D"'
Method: 'POST'
URL: 'https://stream.twitter.com/1.1/statuses/filter.json?delimited=length'

任何帮助都将不胜感激。

编辑:我也尝试使用Twython,我从Twitter获得了相同的回复。

谢谢

也许您可以尝试使用twython oauth2进行连接,如果您不需要发推文Twython auth

或尝试使用此代码。

# Put the consumer keys
auth = tweepy.OAuthHandler("consumer_key", "consumer_secret")
# Redirect user to Twitter to authorize
redirect_user(auth.get_authorization_url())
# Get access token
auth.get_access_token("verifier_value")
# Construct the API instance
api = tweepy.API(auth)
#Create the stream
streamClass= StdOutListener()
stream= tweepy.Stream(auth = api.auth, listener=streamClass)

这应该起作用。也许您的机器人未在帐户中授权,这可能是错误。但我认为情况并非如此。

如果您需要一个示例,请查看我的机器人的验证:ed_postcards bot
(新版本与漂亮的代码传入(

我能够在两周的故障排除后解决问题。对于Tweepy,您需要手动设置用于使用Twitter进行身份验证的Auth对象的回调URL。

此更正后,请访问apps.twitter.com,然后取消选中"允许使用此应用程序与Twitter登录"的框。Twitter API根本不使用错误消息。

最新更新