skip to Main Content

I’ve been trying (and failing) for the life of me to just simply connect to the twitter api using tweepy and post 1 tweet. I have no idea why my code won’t work, it keeps giving me the error that I don’t have a high enough access level. (tweepy.errors.Forbidden: 403 Forbidden
453 – You currently have access to a subset of Twitter API v2 endpoints and limited v1.1 endpoints (e.g. media post, oauth) only.)

import tweepy
from credentials import twitter_bot_keys

auth = tweepy.OAuth1UserHandler(
    twitter_bot_keys['API Key'],
    twitter_bot_keys['API Key Secret'],
    twitter_bot_keys['Access Token'],
    twitter_bot_keys['Access Token Secret']
)

api = tweepy.API(auth)

api.update_status("test")

(Obviously the credentials.py file has my actual credentials in their respective areas)
Any help would be greatly appreciated.

Tried everything, expected it to post a tweet. Also expected it not to be such a nightmare to use this awful API

2

Answers


  1. Chosen as BEST ANSWER

    Used the working code sample from Viktor Brešan in Twitter API access denied for posting a simple tweet

    No idea why that worked instead, but it did so I used that as a base.


  2. Tweepy contributor here,

    Tweepy.org does support both new v2 and v1.1 APIs

    It looks like you are trying to pass Twitter/X’s v2 API credentials instead of v1.1, you can either create new v1.1 credentials from X developer portal or you can use v2 credentials like this

    import tweepy
    
    
    consumer_key = ""
    consumer_secret = ""
    access_token = ""
    access_token_secret = ""
    
    client = tweepy.Client(
        consumer_key=consumer_key, consumer_secret=consumer_secret,
        access_token=access_token, access_token_secret=access_token_secret
    )
    
    # Create Tweet
    
    # The app and the corresponding credentials must have the Write permission
    
    # Check the App permissions section of the Settings tab of your app, under the
    # Twitter Developer Portal Projects & Apps page at
    # https://developer.twitter.com/en/portal/projects-and-apps
    
    # Make sure to reauthorize your app / regenerate your access token and secret 
    # after setting the Write permission
    
    response = client.create_tweet(
        text="This Tweet was Tweeted using Tweepy and Twitter API v2!"
    )
    print(f"https://twitter.com/user/status/{response.data['id']}")
    

    Refer to https://docs.tweepy.org/en/stable/examples.html#examples

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