当前位置: 首页 > 知识库问答 >
问题:

由于服务器时差,python中出现Tweepy 401错误

严安志
2023-03-14

我试图使用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'])

代码可在此处获得

我知道代码中的所有其他内容都是正确的,并且没有语法或身份验证错误。我在问题的答案中读到401错误也是因为服务器-客户端时间差。

我的问题是:我应该将系统时间设置为什么来克服此错误?或者换句话说,我如何知道服务器时间以更改我的系统时间?

共有1个答案

卫博雅
2023-03-14

当您向Twitter API服务器发送HTTP请求时,它会返回一个响应。您应该捕获响应标头,其中包括它们的服务器时间,如下所示:

HTTP/1.1 200 OK
cache-control: no-cache, no-store, must-revalidate, pre-check=0, post-check=0
content-encoding: gzip
content-length: 128
content-security-policy: default-src 'none'; connect-src 'self'; font-src https://abs.twimg.com https://abs-0.twimg.com data:; frame-src 'self' twitter:; img-src https://abs.twimg.com https://*.twimg.com https://pbs.twimg.com data:; media-src 'none'; object-src 'none'; script-src https://abs.twimg.com https://abs-0.twimg.com https://twitter.com https://mobile.twitter.com; style-src https://abs.twimg.com https://abs-0.twimg.com; report-uri https://twitter.com/i/csp_report?a=NVQWGYLXFVWG6Z3JNY%3D%3D%3D%3D%3D%3D&ro=false;
content-type: text/html;charset=utf-8
date: Fri, 23 Mar 2018 22:07:58 GMT
expires: Tue, 31 Mar 1981 05:00:00 GMT
last-modified: Fri, 23 Mar 2018 22:07:58 GMT
ml: S
pragma: no-cache
server: tsa_a
set-cookie: personalization_id="v1_nP/EL/R5y9o+PHvzC1MJ+Q=="; Expires=Sun, 22 Mar 2020 22:07:58 UTC; Path=/; Domain=.twitter.com
set-cookie: guest_id=v1%3A152184287871578374; Expires=Sun, 22 Mar 2020 22:07:58 UTC; Path=/; Domain=.twitter.com
status: 200 OK
strict-transport-security: max-age=631138519
x-connection-hash: 2845e4d99e8f17d11e186a489c475414
x-content-type-options: nosniff
x-frame-options: SAMEORIGIN
x-response-time: 83
x-transaction: 003e14470092d818
x-twitter-response-tags: BouncerCompliant
x-ua-compatible: IE=edge,chrome=1
x-xss-protection: 1; mode=block; report=https://twitter.com/i/xss_report

注意date标头:

date: Fri, 23 Mar 2018 22:07:58 GMT

它使用的是GMT,所以如果你的服务器被设置为不同的时区,调整时差。

我使用了Fiddler并从OAuth请求令牌(https://api.twitter.com/oauth/request_token)endpoint获得了此响应-因此,您不必在执行此操作之前获得授权。

 类似资料:
  • 在启动 Websphere liberty 服务器时注意到以下异常。如何删除它?感谢任何帮助。我们同时安装了javaee7配置文件和webProfile-6.0功能。EAR 应用程序有一个使用 jee6.0 / “3.0” web的 WAR 文件.xml

  • 我在Localhost中正确上传我的文件,但是当我在服务器上上传我的应用程序时,他们给我错误:上传文件后服务器错误。 这是我的控制器: 在我的本地主机中,文件保存在公用文件夹中。我想用public\u html或public\u html/image将我的文件保存在我的服务中。

  • 在启动Eureka服务器时,无法开始获取错误 构建部分 日志:

  • 我刚刚安装了Ruby193和Ruby200,在创建了一个新的应用程序之后,我正在尝试加载服务器,但是我得到了下面的错误。 我用gem'sqlite3'替换了sqlite3 gem,'~ 有人知道怎么修吗? Gemfile是 Bundle Show提供捆绑包中包含的宝石: actionmailer(3.2.13) actionpack(3.2.13) activemodel(3.2.13) acti

  • 尝试在计算机上运行JBoss服务器时出现以下错误: 初始化VM时出错指定的最小和最大堆大小不兼容pickerup_java_options:-xmx512m 我正在使用Windows7。当我查阅它时,我发现问题出在Java参数设置上。我尝试将它设置为-xms512m-xmx1024m,但它不工作(不确定我是否正确)。

  • 问题内容: 我刚刚完成了重新安装操作系统的工作,并且像往常一样安装和测试了我使用的标准工具,现在当我尝试从eclipse启动Jboss 5时,出现了前所未有的错误: 以前有没有人遇到过类似的问题?到目前为止我从未遇到过 问题答案: 看起来像是JRE和OS版本的某些特定组合发生的错误(请参阅https://jira.jboss.org/jira/browse/JBAS-6981)。基本上,JBoss