Twitter API returned a 401 (Unauthorized), Could not authenticate you

966 Views Asked by At

I simply want to post a tweet from my Raspberry Pi and keep getting a 401 error.

-----------------------------------------This is my code-----------------------------------------

import sys
from twython import Twython
consumer_key='x'
consumer_secret='x'
access_token='x'
access_token_secret='x'

twitter = Twython(
   consumer_key,
   consumer_secret,
   access_token,
   access_token_secret
)

twitter.update_status(status='Test')

----------------------------------------Trace back------------------------------------------------

Traceback (most recent call last):
  File "/home/pi/Desktop/Programs/tweetTest.py", line 29, in <module>
    twitter.update_status(status='Test')
  File "/usr/lib/python3/dist-packages/twython/endpoints.py", line 123, in update_status
    return self.post('statuses/update', params=params)
  File "/usr/lib/python3/dist-packages/twython/api.py", line 274, in post
    return self.request(endpoint, 'POST', params=params, version=version)
  File "/usr/lib/python3/dist-packages/twython/api.py", line 264, in request
    api_call=url)
  File "/usr/lib/python3/dist-packages/twython/api.py", line 199, in _request
    retry_after=response.headers.get('X-Rate-Limit-Reset'))
twython.exceptions.TwythonAuthError: Twitter API returned a 401 (Unauthorized), Could not authenticate you
1

There are 1 best solutions below

1
Charles Carriere On

First of all, it appears you have misspelled 'consumer_secret' in your Twython instance: concumer_secret.

Secondly, if you have a desktop app (which I assume you have since this is tagged "raspberry-pi") it looks like you don't need to provide access tokens, just the 'APP_KEY' and 'APP_SECRET'. If you are using the oauth tokens, you might want to provide the code where you are receiving those in case that is the source of the problem.