[SOLVED] Twitter bot using tweepy failed to tweet

Issue

This Content is from Stack Overflow. Question asked by som

I do not speak English well. So I am using a translator. Please understand.

I’m making a twitter bot that automatically tweets via tweepy.
I succeeded in connecting the Twitter app, but it doesn’t tweet.
please help me
I need your help.

This is the code I used.
I referenced the blog.

import tweepy

API_KEY = "key"
API_KEY_SECRET = "key"

auth = tweepy.OAuthHandler(API_KEY, API_KEY_SECRET, callback='oob') 

try: 
    redirect_url = auth.get_authorization_url() 
    print(redirect_url)
except tweepy.TweepError: 
    print('리퀘스트 토큰을 가져오는 데에 실패했습니다.')

pin_number = input("pin number:")
auth.get_access_token(pin_number)

USER_ACCESS_TOKEN = auth.access_token
USER_ACCESS_SECRET= auth.access_token_secret
auth.set_access_token(USER_ACCESS_TOKEN, USER_ACCESS_SECRET)
print(USER_ACCESS_TOKEN)
print(USER_ACCESS_SECRET)
api = tweepy.API(auth)
 
api.update_status("test")

The message posted on the terminal is:

D:user>python test.py
Traceback (most recent call last):
  File "C:UserssomAppDataLocalProgramsPythonPython39libsite-packagestweepyauth.py", line 62, in _get_request_token
    return self.oauth.fetch_request_token(url)
  File "C:UserssomAppDataLocalProgramsPythonPython39libsite-packagesrequests_oauthliboauth1_session.py", line 287, in fetch_request_token
    token = self._fetch_token(url, **request_kwargs)
  File "C:UserssomAppDataLocalProgramsPythonPython39libsite-packagesrequests_oauthliboauth1_session.py", line 369, in _fetch_token
    raise TokenRequestDenied(error % (r.status_code, r.text), r)
requests_oauthlib.oauth1_session.TokenRequestDenied: Token request failed with code 401, response was '{"errors":[{"code":32,"message":"Could not authenticate you."}]}'.

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "C:UserssomAppDataLocalProgramsPythonPython39libsite-packagestweepyauth.py", line 76, in get_authorization_url     
    self.request_token = self._get_request_token(
  File "C:UserssomAppDataLocalProgramsPythonPython39libsite-packagestweepyauth.py", line 64, in _get_request_token        
    raise TweepyException(e)
tweepy.errors.TweepyException: Token request failed with code 401, response was '{"errors":[{"code":32,"message":"Could not authenticate you."}]}'.

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "D:comutest.py", line 9, in <module>
    redirect_url = auth.get_authorization_url()
  File "C:UserssomAppDataLocalProgramsPythonPython39libsite-packagestweepyauth.py", line 81, in get_authorization_url     
    raise TweepyException(e)
tweepy.errors.TweepyException: Token request failed with code 401, response was '{"errors":[{"code":32,"message":"Could not authenticate you."}]}'.

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "D:comutest.py", line 11, in <module>
    except tweepy.TweepError:
AttributeError: module 'tweepy' has no attribute 'TweepError'

I hid my real name and API key. I’ll give it to you if you need it.



Solution

This is due to not authenticating. Error clearly says TokenRequestDenied: Token request failed with code 401, response was '{"errors":[{"code":32,"message":"Could not authenticate you."}]}'.

This means you were not connecting properly to Twitter. Are you using the right keys and token?

EDIT: OP posted wrong error. New error posted by OP shows that he is using a function that is not available in the API: You currently have Essential access which includes access to Twitter API v2 endpoints only. If you need access to this endpoint, you’ll need to apply for Elevated access via the Developer Portal. You can learn more here: https://developer.twitter.com/en/docs/twitter-api/getting-started/about-twitter-api

You need to use tweepy.Client.create_tweet() with this API endpoint.


This Question was asked in StackOverflow by som and Answered by Damiaan It is licensed under the terms of CC BY-SA 2.5. - CC BY-SA 3.0. - CC BY-SA 4.0.

people found this article helpful. What about you?