How do I fix '403 Forbidden' error when using Tweepy to access the Twitter API?
Asked Answered
T

1

-1

I have a Twitter developer account with free access. From my understanding, I’m able to post tweets. But I keep getting an error.

This is the code:

import tweepy, sqlite3 as db, os

# Put your Twitter API keys and stuff here
CONSUMER_KEY = ""
CONSUMER_SECRET = ""
ACCESS_TOKEN = ""
ACCESS_TOKEN_SECRET = ""

auth = tweepy.OAuth1UserHandler(
    CONSUMER_KEY, 
    CONSUMER_SECRET, 
    ACCESS_TOKEN, 
    ACCESS_TOKEN_SECRET
)
api = tweepy.API(auth)

This is the error:

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. If you need access to this endpoint, you may need a different access level. You can learn more here: https://developer.twitter.com/en/portal/product

I tried to regenerate all keys, but still no help.

Tm answered 1/8, 2023 at 9:4 Comment(1)
Error could not be clearer You currently have access to a subset of Twitter API v2 endpoints and limited v1.1 endpoints (e.g. media post, oauth) only. If you need access to this endpoint, you may need a different access level your free tier acces might not include whatever endpoint you're calling.` I't snot saying that the api key is wrong but that it's not enough so regenerating the keys won't change you access level.Fetter
C
2

It seems like you are trying to call the Twitter API v1.1, which can only be used for Media Uploads.

For Tweet Creation, you have to call the Twitter API v2.

Example:

# Authenticate to Twitter
client = tweepy.Client(
    consumer_key=CONSUMER_KEY,
    consumer_secret=CONSUMER_SECRET,
    access_token=ACCESS_TOKEN,
    access_token_secret=ACCESS_TOKEN_SECRET
)

# Post Tweet
message = " MESSAGE "
client.create_tweet(text=message)
print("Tweeted!")

For posting a tweet with an image:

# Authenticate to Twitter
client = tweepy.Client(
    consumer_key=CONSUMER_KEY,
    consumer_secret=CONSUMER_SECRET,
    access_token=ACCESS_TOKEN,
    access_token_secret=ACCESS_TOKEN_SECRET,
)
auth = tweepy.OAuth1UserHandler(
    CONSUMER_KEY,
    CONSUMER_SECRET,
    ACCESS_TOKEN,
    ACCESS_TOKEN_SECRET,
)

# Create API object
api = tweepy.API(auth, wait_on_rate_limit=True)

# Create tweet
message = " MESSAGE "

# Upload image
media = api.media_upload("tweet_img.png")

# Post tweet with image
client.create_tweet(text=message, media_ids=[media.media_id])
print("Tweeted!")

For more information:

Consummate answered 1/8, 2023 at 10:21 Comment(8)
this is the full code: paste.pythondiscord.com/IHGA ..... Full error: snipboard.io/QbVXNl.jpg I don't know what else to do.Tm
@Tm api.update_status_with_media() This endpoint has been DEPRECATED and should no longer be used.Consummate
@Tm Instead, you can use client.create_tweet().Consummate
now this error: snipboard.io/kA1PnC.jpg ..... code: paste.pythondiscord.com/53BATm
@Tm Before calling client.create_tweet(), you need to get the media_id by using api.media_upload() (If you want to post a tweet with an image). Kindly refer to the second code snippet provided in the answer.Consummate
error: snipboard.io/MV24B1.jpg code: paste.pythondiscord.com/OIRATm
@Tm Modify lines 81 and 82 : media = api.media_upload(frame_path) client.create_tweet(text = msg, media_ids = [media.media_id]).Consummate
api.media_upload is now returning 403 Forbidden error for Free users. Is there a v2 API equivalent for this call?Boredom

© 2022 - 2025 — McMap. All rights reserved.