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.
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: