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.

7
  • I would try first authenticating with your apps authentication tokens, skipping the verifier url, just to make sure everything is working as expected.
    – Jonas
    Commented Dec 7, 2015 at 0: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? Commented Dec 7, 2015 at 0: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
    Commented Dec 7, 2015 at 16:01
  • I have the exact same problem, how did you solve it ? Commented Apr 18, 2017 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
    Commented Oct 5, 2018 at 15:32

3 Answers 3

1

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

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.

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.