skip to Main Content

This code keeps returning the 403 forbidden error. I have no clue how to fix this because all of the other examples 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")

2

Answers


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

    Login or Signup to reply.
  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/

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search