【发布时间】:2018-08-31 05:46:53
【问题描述】:
我正在尝试使用 Tweepy 检索 Twitter 数据,使用下面的代码,但它返回 401 错误,我重新生成了访问和秘密令牌,但没有任何区别
#Import the necessary methods from tweepy library
from tweepy.streaming import StreamListener
from tweepy import OAuthHandler
from tweepy import Stream
#Variables that contains the user credentials to access Twitter API
access_token = "ENTER YOUR ACCESS TOKEN"
access_token_secret = "ENTER YOUR ACCESS TOKEN SECRET"
consumer_key = "ENTER YOUR API KEY"
consumer_secret = "ENTER YOUR API SECRET"
#This is a basic listener that just prints received tweets to stdout.
class StdOutListener(StreamListener):
def on_data(self, data):
print data
return True
def on_error(self, status):
print status
if __name__ == '__main__':
#This handles Twitter authetification and the connection to Twitter Streaming API
l = StdOutListener()
auth = OAuthHandler(consumer_key, consumer_secret)
auth.set_access_token(access_token, access_token_secret)
stream = Stream(auth, l)
#This line filter Twitter Streams to capture data by the keywords: 'python', 'javascript', 'ruby'
stream.filter(track=['python', 'javascript', 'ruby'])
代码可用here
我知道代码中的所有其他内容都是正确的,并且没有语法或身份验证错误。我在question 的答案中读到,401 错误也是由于服务器-客户端时差造成的。
我的问题是:我应该将系统时间设置为什么来克服此错误?或者换句话说,我如何知道服务器时间以便更改我的系统时间?
【问题讨论】:
-
看起来像这个开放的错误:401 Unauthorized when streaming
标签: python twitter tweepy http-status-code-401