Reputation: 21
So, I wrote a code for a Twitter Bot that tweets "YES" on every thursday. However, when i run the code i get the following error:
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
This is the code that i wrote
import tweepy
import datetime
import time
consumer_key = 'XXXXXXXXXXXXXXXXXXXXXXXXX'
consumer_secret = 'XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX'
access_token = 'XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX'
access_token_secret = 'XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX'
auth = tweepy.OAuthHandler(consumer_key, consumer_secret)
auth.set_access_token(access_token, access_token_secret)
api = tweepy.API(auth)
def is_giovedi():
return datetime.datetime.now().strftime('%A') == 'Thursday'
def post_tweet():
if is_giovedi():
tweet = "SI"
else:
tweet = "NO"
api.update_status(status=tweet)
if __name__ == "__main__":
now = datetime.datetime.now()
target_time = now.replace(hour=12, minute=0, second=0, microsecond=0)
if now < target_time:
time_to_wait = (target_time - now).seconds
print(f"Aspetto {time_to_wait} secondi")
time.sleep(time_to_wait)
post_tweet()
print("Tweet inviato con successo!")
Hope someone can help me
I put the keys that the code asked me from the twitter developer portal, but when i run the code it gives me this error
Upvotes: 1
Views: 5341
Reputation: 516
It seems like you tried to call the Twitter API v1.1, which can only be used for Media Uploads.
For posting a tweet, you should call the Twitter API v2.
Replace:
auth = tweepy.OAuthHandler(consumer_key, consumer_secret)
auth.set_access_token(access_token, access_token_secret)
api = tweepy.API(auth)
with the following code snippet:
# Authenticate to Twitter
client = tweepy.Client(
consumer_key=consumer_key,
consumer_secret=consumer_secret,
access_token=access_token,
access_token_secret=access_token_secret
)
And, also replace:
api.update_status(status=tweet)
with the following code snippet:
# Post Tweet
client.create_tweet(text=tweet)
For more information on Twitter API v2:
https://developer.twitter.com/en/docs/twitter-api
https://docs.tweepy.org/en/v4.14.0/client.html#
I hope this helps.
Upvotes: 4