question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Twitter API returned a 401 (Unauthorized), Invalid or expired token

See original GitHub issue

Similar to this comment by hako, I get an 401 error trying to access the timeline. In a later comment he writes that it was caused by a problem with his access token and that he fixed it. But he does not explain how.

Here is what I have tried:

from twython import Twython
APP_KEY = 'XXX'
APP_SECRET = 'XXX'

twitter = Twython(APP_KEY, APP_SECRET)
auth = twitter.get_authentication_tokens()
OAUTH_TOKEN = auth['oauth_token']
OAUTH_TOKEN_SECRET = auth['oauth_token_secret']
print auth['auth_url']  
# I manually open this url in the browers and
# set oaut_verifier to the value like seen below.
oauth_verifier = u'xxxx'
twitter = Twython(APP_KEY, APP_SECRET, OAUTH_TOKEN, OAUTH_TOKEN_SECRET)

final_step = twitter.get_authorized_tokens(oauth_verifier)

OAUTH_TOKEN = final_step['oauth_token']
OAUTH_TOKEN_SECERT = final_step['oauth_token_secret']

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

And this is the error message:

twitter.verify_credentials()
---------------------------------------------------------------------------
TwythonAuthError                          Traceback (most recent call last)
<ipython-input-67-439282f495ea> in <module>()
----> 1 twitter.verify_credentials()

/usr/local/lib/python2.7/dist-packages/twython/endpoints.pyc in verify_credentials(self, **params)
    321 
    322         """
--> 323         return self.get('account/verify_credentials', params=params)
    324 
    325     def update_account_settings(self, **params):

/usr/local/lib/python2.7/dist-packages/twython/api.pyc in get(self, endpoint, params, version)
    228     def get(self, endpoint, params=None, version='1.1'):
    229         """Shortcut for GET requests via :class:`request`"""
--> 230         return self.request(endpoint, params=params, version=version)
    231 
    232     def post(self, endpoint, params=None, version='1.1'):

/usr/local/lib/python2.7/dist-packages/twython/api.pyc in request(self, endpoint, method, params, version)
    222             url = '%s/%s.json' % (self.api_url % version, endpoint)
    223 
--> 224         content = self._request(url, method=method, params=params, api_call=url)
    225 
    226         return content

/usr/local/lib/python2.7/dist-packages/twython/api.pyc in _request(self, url, method, params, api_call)
    192             raise ExceptionType(error_message,
    193                                 error_code=response.status_code,
--> 194                                 retry_after=response.headers.get('retry-after'))
    195 
    196         # if we have a json error here, then it's not an official Twitter API error

TwythonAuthError: Twitter API returned a 401 (Unauthorized), Invalid or expired token

The twython version is:

In [70]: twython.__version__
Out[70]: '3.1.2'

Issue Analytics

  • State:closed
  • Created 10 years ago
  • Comments:11

github_iconTop GitHub Comments

2reactions
hakocommented, Jan 17, 2014

Try this, I modified your code a bit,


from twython import Twython
APP_KEY = ''
APP_SECRET = ''

twitter = Twython(APP_KEY, APP_SECRET)

auth = twitter.get_authentication_tokens()

OAUTH_TOKEN = auth['oauth_token']

OAUTH_TOKEN_SECRET = auth['oauth_token_secret']

print auth['auth_url']

# I manually open this url in the browers and
# set oaut_verifier to the value like seen below.

oauth_verifier = raw_input('Enter your pin:')

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

final_step = twitter.get_authorized_tokens(oauth_verifier)

FINAL_OAUTH_TOKEN = final_step['oauth_token']
FINAL_OAUTH_TOKEN_SECRET = final_step['oauth_token_secret']

twitter = Twython(APP_KEY, APP_SECRET,
                  FINAL_OAUTH_TOKEN, FINAL_OAUTH_TOKEN_SECRET)

print twitter.verify_credentials()

Sorry I didn’t elaborate my issue, it was saving the access token using pin based oauth.

This should work for you 😃

hako

0reactions
aimfgscommented, Apr 5, 2021

Try this, I modified your code a bit,

from twython import Twython
APP_KEY = ''
APP_SECRET = ''

twitter = Twython(APP_KEY, APP_SECRET)

auth = twitter.get_authentication_tokens()

OAUTH_TOKEN = auth['oauth_token']

OAUTH_TOKEN_SECRET = auth['oauth_token_secret']

print auth['auth_url']

# I manually open this url in the browers and
# set oaut_verifier to the value like seen below.

oauth_verifier = raw_input('Enter your pin:')

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

final_step = twitter.get_authorized_tokens(oauth_verifier)

FINAL_OAUTH_TOKEN = final_step['oauth_token']
FINAL_OAUTH_TOKEN_SECRET = final_step['oauth_token_secret']

twitter = Twython(APP_KEY, APP_SECRET,
                  FINAL_OAUTH_TOKEN, FINAL_OAUTH_TOKEN_SECRET)

print twitter.verify_credentials()

Sorry I didn’t elaborate my issue, it was saving the access token using pin based oauth.

This should work for you 😃

hako

Worked well for us at www.aimfgs.com!

Read more comments on GitHub >

github_iconTop Results From Across the Web

Twitter API returned a 401 (Unauthorized), Invalid or expired ...
I would try first authenticating with your apps authentication tokens, skipping the verifier url, just to make sure everything is working as ...
Read more >
401 unauthorized access when trying to fetch public tweets
I'm having trouble authenticating my requests to the API. ... saying “Twitter API returned a 401 (Unauthorized), Invalid or expired token.
Read more >
Twitter API Response Codes & Error Support - Twitter Developer
Different error codes indicate different reasons for an error. The Twitter API attempts to return appropriate HTTP status codes for every request.
Read more >
Error 401, "unauthorized" while trying to connect to Twitter's ...
You have the right idea in terms of getting started. A 401 error usually mean there was a problem authenticating your request. From...
Read more >
'401 Unauthorized HTTP' for REST API token that expires in ...
For the Fuel/REST API, access tokens expire one hour after they are issued, when you use a legacy package. If you attempt to...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found