I am trying to access the Twitter API v2 both through the API directly on Postman and Tweepy in Python on macOS, but I keep encountering a 401 Unauthorized error. Despite using what I believe to be are correct tokens my requests are not being authenticated. I have regenerated these keys multiple times and still am encountering the same error.
Tried:
import tweepy
api_key = ""
api_secret = ""
bearer_token = ""
access_token = ""
access_token_secret = ""
client = tweepy.Client(bearer_token, api_key, api_secret, access_token,access_token_secret)
auth = tweepy.OAuth1UserHandler(api_key, api_secret, access_token, access_token_secret)
api = tweepy.API(auth)
# Function to like a tweet
def like_tweet(tweet_id):
try:
client.like(tweet_id)
print(f"Successfully liked tweet {tweet_id}")
except tweepy.TweepyException as e:
print(f"Error: {e}")
# Like the tweet with the specified ID
like_tweet(1813606495118062034)
Result:
diyagirishkumar@Diyas-MacBook-Air twitterbot % /usr/local/b in/python3 /Users/diyagirishkumar/twitterbot/tweepy_setup.p y Error: 403 Forbidden When authenticating requests to the Twitter API v2 endpoints, you must use keys and tokens from a Twitter developer App that is attached to a Project. You can create a project via the developer portal.
On Postman:
{ "title": "Unauthorized", "type": "about:blank", "status": 401, "detail": "Unauthorized" }
Diya Girish Kumar is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.