由于服务器时间差异导致python中的Tweepy 401错误

时间:2020-12-03 15:35:30

I am trying to retrieve Twitter data using Tweepy, using that below code, but it returns 401 error, and I regenerated the access and secret tokens but didnt make a difference

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

The code is available here

代码可在此处获得

I know for a fact that all the other things in the code are right and there are no syntax or authentication error. I read in an answer for the question that 401 error is also because of server-client time difference.

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

My question is: To what do I set my system time to overcome this error? Or in other words how do I know the server time so as to change my system time?

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

1 个解决方案

#1


0  

When you send an HTTP request to the Twitter API server, it will send a response back. You should capture the response headers, which will include their server time, like this:

当您向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

notice the date header:

注意日期标题:

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

It's using GMT, so if your server is set to a different time zone, adjust for the time difference.

它使用GMT,因此如果您的服务器设置为不同的时区,请调整时差。

I used Fiddler and got this response from the OAuth request token (https://api.twitter.com/oauth/request_token) endpoint - so, you don't have to be authorized before being able to do this.

我使用Fiddler并从OAuth请求令牌(https://api.twitter.com/oauth/request_token)端点获得此响应 - 因此,您无需获得授权,然后才能执行此操作。

#1


0  

When you send an HTTP request to the Twitter API server, it will send a response back. You should capture the response headers, which will include their server time, like this:

当您向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

notice the date header:

注意日期标题:

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

It's using GMT, so if your server is set to a different time zone, adjust for the time difference.

它使用GMT,因此如果您的服务器设置为不同的时区,请调整时差。

I used Fiddler and got this response from the OAuth request token (https://api.twitter.com/oauth/request_token) endpoint - so, you don't have to be authorized before being able to do this.

我使用Fiddler并从OAuth请求令牌(https://api.twitter.com/oauth/request_token)端点获得此响应 - 因此,您无需获得授权,然后才能执行此操作。