5

I am using the below code to post a Tweet. This is a pretty standard authentication procedure but for some reason I cannot authenticate. The error I am getting is Twitter API returned a 401 (Unauthorized), Invalid or expired token.

from twython import Twython, TwythonError
import requests

APP_KEY = 'rpOzpgp2FZNJqsq0' #fake key
APP_SECRET = 'FKBJWXOJwXTblhi1xBl4PtKgPemNFvumH' #fake secret

twitter = Twython(APP_KEY, APP_SECRET)
auth = twitter.get_authentication_tokens()

OAUTH_TOKEN = auth['oauth_token']
OAUTH_TOKEN_SECRET = auth['oauth_token_secret']

twitter = Twython(APP_KEY, APP_SECRET, OAUTH_TOKEN, OAUTH_TOKEN_SECRET)

oauth_verifier_url = auth['auth_url']
oauth_verifier = requests.get(oauth_verifier_url)

# Getting the FINAL authentication tokens
final_step = twitter.get_authorized_tokens(oauth_verifier)

OAUTH_TOKEN = final_step['oauth_token']
OAUTH_TOKEN_SECRET = auth['oauth_token_secret']

twitter = Twython(APP_KEY, APP_SECRET, OAUTH_TOKEN, OAUTH_TOKEN_SECRET)

twitter.update_status(status='Yo')

Here is the full error message:

Traceback (most recent call last):
  File "test.py", line 20, in <module>
    final_step = twitter.get_authorized_tokens(oauth_verifier)
  File "/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/site-packages/twython/api.py", line 380, in get_authorized_tokens
    ken'), error_code=response.status_code)
twython.exceptions.TwythonError: Twitter API returned a 401 (Unauthorized), Invalid / expired To             ken

Why could I be getting this error and what can I do to fix it?

I have tried regenerating my keys multiple times. I have even deleted my app and created new ones multiple times but I still keep getting this error.

Under Application Settings my access level is set to read and write:

Screenshot of app page

I am not behind a firewall.

I have read other solutions concerning Twython authentication on this site but they all seem to provide the above code as the solution but this itself is not working for me.

Adriaan Joubert
  • 185
  • 1
  • 4
  • 15
  • I would try first authenticating with your apps authentication tokens, skipping the verifier url, just to make sure everything is working as expected. – Jonas Dec 07 '15 at 00:21
  • Thank you for suggesting that! That does work. I am pretty sure I am using the correct app keys and secrets and I have regenerated these multiple times but I still am unable to get the auth tokens this way. Any idea why? – Adriaan Joubert Dec 07 '15 at 00:26
  • Check the returned authentication tokens, to see if they look right. Also, try logging in using the apps account and a different account. – Jonas Dec 07 '15 at 16:01
  • I have the exact same problem, how did you solve it ? – bluesummers Apr 18 '17 at 10:20
  • I find kind of confusing passing the result of a requests.get() as a parameter to a method like get_authorized_tokens(). If you type help(twitter.get_authorized_tokens) it tells this should be a verifier or PIN and the result of requests.get() is a whole object with HTML, headers and so on. Are you sure you're doing that step right? – madtyn Oct 05 '18 at 15:32
  • I have the same problem so I arrived here like you, but that doesn't feel right for me. Can anyone tell me if I'm wrong? – madtyn Oct 05 '18 at 15:33
  • did you find a solution to this? – mor222 Jun 28 '20 at 17:21

3 Answers3

1

I was facing the same problem, but after regeneration of keys the problem got solved.

Ritesh
  • 11
  • 1
0

Seems that you're using the Twitter API too often. Due to security reasons, Twitter has rate limiting on the usage of their Twitter API from an arbitrary app. Twitter says in their post how their Twitter API rate-limiting works. You can make a maximum of 15 GET requests under 15 minutes from a specific set of authorization keys of that particular app. After 15 minutes, you are free to make the next 15 requests available.

gre_gor
  • 6,669
  • 9
  • 47
  • 52
0

You need to send four arguments not only 2

Objtwython = Twython(APP_KEY,APP_SECRET,ACCESS_TOKEN,ACCESS_TOKEN_SECRET)

last ones the same way you get the others, getting from twitter directly.

Jatin Mehrotra
  • 9,286
  • 4
  • 28
  • 67