Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

stream.statuses.filter returns Error 401 Unauthorized suddenly #285

Open
lxh529 opened this issue Mar 2, 2015 · 16 comments
Open

stream.statuses.filter returns Error 401 Unauthorized suddenly #285

lxh529 opened this issue Mar 2, 2015 · 16 comments

Comments

@lxh529
Copy link

lxh529 commented Mar 2, 2015

I have been using this great api for tracking some twitter keywords for a while, but suddenly, since Feb 24 around 13:00 I got "Error 401 Unauthorized" error.

I am using 1.14.3. Any body help? Thanks!

@minuscorp
Copy link

This is happening to me as well, I'm using 1.17.1 with the same error.

Can somebody help?

Traceback (most recent call last):
  File "TwitterExtractor.py", line 378, in <module>
    twitterExtractor.stream_extract(keywords="incibe")
  File "TwitterExtractor.py", line 249, in stream_extract
    tweet_iterator = stream.statuses.filter(track=str(keywords), language="es")
  File "/home/jrevuelta/venv/local/lib/python2.7/site-packages/twitter/api.py", line 312, in __call__
    return self._handle_response(req, uri, arg_data, _timeout)
  File "/home/jrevuelta/venv/local/lib/python2.7/site-packages/twitter/stream.py", line 284, in _handle_response
    _timeout or timeout, heartbeat_timeout)
  File "/home/jrevuelta/venv/local/lib/python2.7/site-packages/twitter/stream.py", line 209, in handle_stream_response
    raise TwitterHTTPError(e, uri, 'json', arg_data)
twitter.api.TwitterHTTPError: Twitter sent status 401 for URL: 1.1/statuses/filter.json using parameters: (language=es&oauth_consumer_key=XXXXXXXXXXX&oauth_nonce=1529571930533329411&oauth_signature_method=HMAC-SHA1&oauth_timestamp=1438337641&oauth_token=XXXXXXXXXXXXXXXXX&oauth_version=1.0&track=incibe&oauth_signature=XXXXXXXXXXXXXXXX)
details: <html>\n<head>\n<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>\n<title>Error 401 Unauthorized</title>
</head>
<body>
<h2>HTTP ERROR: 401</h2>
<p>Problem accessing '/1.1/statuses/filter.json'. Reason:
<pre>    Unauthorized</pre>
</body>
</html>

@RouxRC
Copy link
Member

RouxRC commented Jul 31, 2015

This looks to me like a badly documented issue regarding OAuth and timestamps: if that's the case, basically your server is slighty shifted from the actual time and when Twitter gets your OAuth timestamped queries they're considered outdated. This typically happens sometimes after leapSecond changes if the time is not properly fixed on the server.
So can you try and update the time on your machine and see whether that solves it?
Otherwise it might be a simple auth issue, maybe try to regen and change your tokens/secret on dev.twitter.com

@minuscorp
Copy link

Actually that was true.

My server has (don't know why) a time drift, I synced it again with an NTP Server and it worked again.

Thank you so much!

@RouxRC
Copy link
Member

RouxRC commented Jan 4, 2016

keeping this one open as documentation for further people

@sunny074u
Copy link

Please help, I faced this problem;

\n\n\n<title>Error 401 Unauthorized</title>

HTTP ERROR: 401

Problem accessing '/1/statuses/sample.json?oauth_body_hash=2jmj7l5rSw0yVb%2FvlWAYkK%2FYBwk%3D&oauth_nonce=49357639&oauth_timestamp=1476141985&oauth_consumer_key=%3CkahjO7UXvguegErgJZBNLHmO3%3E&oauth_signature_method=HMAC-SHA1&oauth_version=1.0&oauth_token=%3C340989397-wnPAEy0p0L2VvyYAYOR75ATdnKNL5eX6QFg3RhbE%3E&oauth_signature=4TU60zYSGpEZqAYaAEJlEs0F47Y%3D'. Reason:

    Unauthorized

@RouxRC
Copy link
Member

RouxRC commented Oct 23, 2016

Hi @sunny074u, looking at the keys in your url, it looks like you're using < (%3c) > (%3e) symbols around your keys in your oauth setting.

@nedd-bastien
Copy link

Well thanks, I had a 20min time drift & was going mad :)
(aptitude install ntp, on debian, did the trick)

@RouxRC
Copy link
Member

RouxRC commented Jun 28, 2017

As a reminder, when this happens, a typical resolution on linux machines is:

sudo service ntp stop
sudo ntpd -gq
# Or on older machines
# sudo ntpdate -s time.nist.gov
sudo service ntp start

@n-eq
Copy link

n-eq commented Jul 2, 2020

Hi @RouxRC !

I am running into the same issue and it doesn't seem it's a time drift problem (I executed the ntp script but it didn't change anything.)

Any updates?

@RouxRC
Copy link
Member

RouxRC commented Jul 2, 2020

Hasn't your account/API keyset been suspended by twitter maybe? These are the main two possible reasons I experienced

@n-eq
Copy link

n-eq commented Jul 2, 2020

Hi @RouxRC
Yes, I double checked. I used another api (tweepy) to test tweeting and it actually works so the keys must be valid. Actually even the basic operations don't work (e.g. home_timeline returns 404). I'll check again if there's something wrong but for the record here's the log :

>>> t = TwitterStream(auth=OAuth("XX", "XX", "XX", "XX"))
>>> t
<twitter.stream.TwitterStream object at 0x7f888f6782d0>
>>> t.statuses.sample()
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "twitter/api.py", line 334, in __call__
    return self._handle_response(req, uri, arg_data, _timeout)
  File "twitter/stream.py", line 264, in _handle_response
    _timeout or timeout, heartbeat_timeout)
  File "twitter/stream.py", line 213, in handle_stream_response
    raise TwitterHTTPError(e, uri, 'json', arg_data)
twitter.api.TwitterHTTPError: Twitter sent status 401 for URL: 1.1/statuses/sample.json using parameters: (oauth_consumer_key=XX&oauth_nonce=XX&oauth_signature_method=HMAC-SHA1&oauth_timestamp=1593696259&oauth_token=XX&oauth_version=1.0&oauth_signature=pCmGt%2BrmWppCCCewrpGezi0Acb8%3D)
details: <html>\n<head>\n<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>\n<title>Error 401 Unauthorized</title>
</head>
<body>
<h2>HTTP ERROR: 401</h2>
<p>Problem accessing '/1.1/statuses/sample.json?oauth_consumer_key=XX&amp;oauth_nonce=XX&amp;oauth_signature_method=HMAC-SHA1&amp;oauth_timestamp=1593696259&amp;oauth_token=RRVD4z4jcFLCfaORJpVGmCFTW&amp;oauth_version=1.0&amp;oauth_signature=pCmGt%2BrmWppCCCewrpGezi0Acb8%3D'. Reason:
<pre>    Unauthorized</pre>
</body>
</html>

@RouxRC
Copy link
Member

RouxRC commented Jul 6, 2020

Hi,
All examples output you've shown are 401 on stream routes, can you show other routes with the same auth?
For instance what does t.search.tweets(q="test") return?

@n-eq
Copy link

n-eq commented Jul 6, 2020

@RouxRC you're right.
Here's the output of the command you requested.

>>> t = Twitter(auth=OAuth('xx-wa4vYbh7sFg7dTlUZ4dq7fUGjgGhwz', 'xx', 'xx', 'xx'))
>>> t.statuses.sample()
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "twitter/api.py", line 334, in __call__
    return self._handle_response(req, uri, arg_data, _timeout)
  File "twitter/api.py", line 367, in _handle_response
    raise TwitterHTTPError(e, uri, self.format, arg_data)
twitter.api.TwitterHTTPError: Twitter sent status 404 for URL: 1.1/statuses/sample.json using parameters: (oauth_consumer_key=xx&oauth_nonce=114329492091994170&oauth_signature_method=HMAC-SHA1&oauth_timestamp=1594070658&oauth_token=xx-wa4vYbh7sFg7dTlUZ4dq7fUGjgGhwz&oauth_version=1.0&oauth_signature=Jhkm69Avymum2taLySZ%2BxNaEFU4%3D)
details: {u'errors': [{u'message': u'Sorry, that page does not exist', u'code': 34}]}
>>> t.search.tweets(q='test')
{u'search_metadata': {u'count': 15, u'completed_in': 0.059, u'max_id_str': u'1280251196398329857', u'since_id_str': u'0', u'next_results': u'?max_id=1280251182687158272&q=test&include_entities=1', u'refresh_url': u'?since_id=1280251196398329857&q=test&include_entities=1', u'since_id': 0, u'query': u'test', u'max_id': 1280251196398329857}, u'statuses': [{u'contributors': None, u'truncated': True, u'text': u'@Will_BrysonMVCS @Sooner_Source And they are counting tests as positive if you have an appointment then leave befor\u2026 https://t.co/qkJGFakdR8', u'is_quote_status': False, u'in_reply_to_status_id': 1280248041434492928, u'id': 1280251196398329857, u'favorite_count': 0, u'entities': {u'symbols': [], u'user_mentions': [{u'id': 778229275015118848, u'indices': [0, 16], u'id_str': u'778229275015118848', u'screen_name': u'Will_BrysonMVCS', u'name': u'Coach Bryson'}, {u'id': 278899813, u'indices': [17, 31], u'id_str': u'278899813', u'screen_name': u'Sooner_Source', u'name': u'SoonerSource'}], u'hashtags': [], u'urls': [{u'url': u'https://t.co/qkJGFakdR8', u'indices': [117, 140], u'expanded_url': u'https://twitter.com/i/web/status/1280251196398329857', u'display_url': u'twitter.com/i/web/status/1\u2026'}]}, u'retweeted': False, u'coordinates': None, u'source': u'<a href="http://twitter.com/download/iphone" rel="nofollow">Twitter for iPhone</a>', u'in_reply_to_screen_name': u'Will_BrysonMVCS', u'in_reply_to_user_id': 778229275015118848, u'retweet_count': 0, u'id_str': u'1280251196398329857', u'favorited': False, u'user': {u'follow_request_sent': False, u'has_extended_profile': True, u'profile_use_background_image': True, u'default_profile_image': False, u'id': 904837484953165824, u'profile_background_image_url_https': None, u'verified': False, u'translator_type': u'none', u'profile_text_color': u'333333', u'profile_image_url_https': u'https://pbs.twimg.com/profile_images/1267662209557151747/QQ3YXw_-_normal.jpg', u'profile_sidebar_fill_color': u'DDEEF6', u'entities': {u'description': {u'urls': []}}, u'followers_count': 1200, u'profile_sidebar_border_color': u'C0DEED', u'id_str': u'904837484953165824', u'profile_background_color': u'F5F8FA', u'listed_count': 1, u'is_translation_enabled': False, u'utc_offset': None, u'statuses_count': 15284, u'description': u'Basketball Enthusiast \u2022 Christ Follower \u2022 @tristentamera significant other \u2764\ufe0f', u'friends_count': 1841, u'location': u'Mustang, OK', u'profile_link_color': u'1DA1F2', u'profile_image_url': u'http://pbs.twimg.com/profile_images/1267662209557151747/QQ3YXw_-_normal.jpg', u'following': False, u'geo_enabled': True, u'profile_banner_url': u'https://pbs.twimg.com/profile_banners/904837484953165824/1589757129', u'profile_background_image_url': None, u'screen_name': u'CodyJohnsonBB', u'lang': None, u'profile_background_tile': False, u'favourites_count': 19320, u'name': u'Coach Johnson', u'notifications': False, u'url': None, u'created_at': u'Mon Sep 04 22:44:02 +0000 2017', u'contributors_enabled': False, u'time_zone': None, u'protected': False, u'default_profile': True, u'is_translator': False}, u'geo': None, u'in_reply_to_user_id_str': u'778229275015118848', u'lang': u'en', u'created_at': u'Mon Jul 06 21:24:05 +0000 2020', u'in_reply_to_status_id_str': u'1280248041434492928', u'place': None, u'metadata': {u'iso_language_code': u'en', u'result_type': u'recent'}}]}

I trimmed the json output for better readability, but the command succeeds.

@RouxRC
Copy link
Member

RouxRC commented Jul 6, 2020

Ha all right I think I know: you're not using the proper class for Stream routes, you should use t=TwitterStream(auth=OAuth(...)) instead

@RouxRC
Copy link
Member

RouxRC commented Jul 6, 2020

PS : and it's normal that home_timeline returns a 404, it was shut down by Twitter a few months ago

@n-eq
Copy link

n-eq commented Jul 6, 2020

Ha all right I think I know: you're not using the proper class for Stream routes, you should use t=TwitterStream(auth=OAuth(...)) instead

Indeed, works like a charm now. Thanks @RouxRC !

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

6 participants