Twitter bot using tweepy failed to tweet

Question:

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:comu>python test.py
https://api.twitter.com/oauth/authorize?oauth_token=HfAjGgAAAAABf1z6AAABg1WDXtg
핀 번호:6602485
1483679334808227841-PM2TN5Upj6q400SYcb9IkJvdS1QSAT
6ymidpKExwvjYu6HGon9dQ9yzoyLQq7v44vcq31367fka
Traceback (most recent call last):
  File "D:comutest.py", line 32, in <module>
    api.update_status("트윗 테스트")
  File "C:UserssomAppDataLocalProgramsPythonPython39libsite-packagestweepyapi.py", line 46, in wrapper
    return method(*args, **kwargs)
  File "C:UserssomAppDataLocalProgramsPythonPython39libsite-packagestweepyapi.py", line 1136, in update_status
    return self.request(
  File "C:UserssomAppDataLocalProgramsPythonPython39libsite-packagestweepyapi.py", line 259, in request
    raise Forbidden(resp)
tweepy.errors.Forbidden: 403 Forbidden
453 - 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#v2-access-leve

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

`+ Sorry, I posted a code error message for not entering a key. It’s corrected.

`+ As the comment said, part of the try statement was changed.
The changed code is as follows.

# 4. 엑세스 권한 요청 url 
try: # 성공 했을 때
    redirect_url = auth.get_authorization_url() 
    print(redirect_url)
except tweepy.TweepException: # 에러 떴을 때 
    print('리퀘스트 토큰을 가져오는 데에 실패했습니다.')

D:comu>python test.py
https://api.twitter.com/oauth/authorize?oauth_token=jS3NdAAAAAABf1z6AAABg1WJyZ0
핀 번호:9388267
1567389027296509958-LJ7Gtw4krDyVjuKmFQXIou8x4RPuuA
cZFyQCUZzCleS1HD6nIDZltbkjKlA7RTcDLOQjoi72QHI
Traceback (most recent call last):
  File "D:comutest.py", line 32, in <module>
    api.update_status("트윗 테스트")
  File "C:UserssomAppDataLocalProgramsPythonPython39libsite-packagestweepyapi.py", line 46, in wrapper
    return method(*args, **kwargs)
  File "C:UserssomAppDataLocalProgramsPythonPython39libsite-packagestweepyapi.py", line 1136, in update_status
    return self.request(
  File "C:UserssomAppDataLocalProgramsPythonPython39libsite-packagestweepyapi.py", line 259, in request
    raise Forbidden(resp)
tweepy.errors.Forbidden: 403 Forbidden
453 - 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#v2-access-leve

And this error was written in the terminal. I still can’t tweet.

Asked By: som

||

Answers:

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.

Answered By: Damiaan
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.