0

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

#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.

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 Answers1

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:

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.

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.

Joe Mayo
  • 7,501
  • 7
  • 41
  • 60