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" }
2
Answers
Because of the level of access, If you are on the free version, you can create or delete just the information of your tweets for exemple.
You can check this documentation : https://developer.x.com/en/portal/products/basic
I also found the same error , And spent a week in researching about the error , And after checking a node.js sdk I found that I was missing a User-Agent Headers;
please add this headers and try