I am trying the following code but it gives 401 error:
headers = {
"Authorization": f"OAuth oauth_consumer_key={consumer_key},oauth_token={access_token},oauth_signature_method=HMAC-SHA1"
}
# Create data payload for the tweet with attached media
data = {
"text": text,
"media_ids": [media_id],
"tweet_mode": "extended"
}
tweet_response = requests.post(tweet_post_url, headers=headers, data=data)
print(tweet_response.json())
You can get your desired result by following the sample code provided below:
Source: https://github.com/twitterdev/Twitter-API-v2-sample-code/blob/main/Manage-Tweets/create_tweet.py
I hope this helps.