我正在尝试连接到twitter流api,以便实时获取tweet。这段代码直到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"])到目前为止,我已经尝试了以下几点:
以下是请求:
'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'任何帮助将不胜感激,为什么我要得到401未经授权的“需要授权”的回应。
编辑:我也尝试使用Twython,我从twitter上得到了同样的回复。
谢谢
发布于 2017-04-27 10:33:05
经过两周的故障排除后,我终于解决了这个问题。对于tweepy,您需要手动设置用于使用twitter进行身份验证的auth对象的回调url。
在此更正后,将头转到apps.twitter.com,并取消复选框“允许使用此应用程序登录Twitter”。对于错误消息,Twitter一点也不简洁。
发布于 2017-04-21 08:25:48
如果您不需要回推吐温,也许可以尝试使用Twython OAuth2进行连接。
或者尝试使用这段代码。
# 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)这应该能行。也许你的机器人没有在帐户上被授权,这可能是错误。但我不认为是这样。
如果你想要一个例子的话,看看我的机器人的auth:明信片机器人
(带有漂亮代码的新版本)
https://stackoverflow.com/questions/43518290
复制相似问题