'I am trying to use twitter's V2.0 api for a python tweepy bot. But I keep getting the forbidden error

This code keeps returning the 403 forbidden error. I have no clue how to fix this because all of the other example's I've looked at are for the v1.0 api. It says I need "Elevated access" to use this endpoint but I should be able to post a tweet with the essential access I have now. This is confusing and I would love some help!

    import tweepy
    
    consumer_key = 'consumer_key'
    consumer_secret = 'consumer_secret'
    access_token = 'access_token'
    access_token_secret = 
    'access_token_secret'
    
    auth = 
    tweepy.OAuthHandler(consumer_key, 
    consumer_secret)
    auth.set_access_token(access_token, 
    access_token_secret)
    
    api = tweepy.API(auth)

    api.update_status("first tweet")


Solution 1:[1]

Use tweepy.Client to access API v2 (not tweepy.API)

Solution 2:[2]

You can use following code:

import tweepy

client = tweepy.Client( consumer_key= "<your consumer/API key - see step 1>",
                        consumer_secret= "<your consumer/API secret - see step 1>",
                        access_token= "<your access token key - see step 1>",
                        access_token_secret= "<your access token secret - see step 1>")

#construct the tweet text
tweet_text =  f"Hello World" 

#Fire off the tweet!
response = client.create_tweet( tweet_text )

You can see full step by step article here: https://pythonhowtoprogram.com/how-to-build-a-twitter-bot-with-python-and-twitter-api-v2/

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Andy Piper
Solution 2 charlesw001