(Part 4) Tweeting with Twitter Access Token
Part 4 of the Twitter OAuth 2.0 series. We will learn how to post and also delete a tweet using the Twitter API.
Tags
It’s high time we get to tweeting! 🐦
TLDR 🥱!!!
You’ve got the Access Token or Bearer Token from the previous part of this series.
Now, what the fu*c are you waiting for? Just go nuts and tweet! 🫡
Check out the docs Twitter API or POST/2/Tweets for endpoints.
Posting a Tweet 📝
I’m using Insomnia to make the API requests, because I’m tired of writing code for everything. 😒
-
Here’s the endpoint I’ll make a POST request to :-
https://api.twitter.com/2/tweets
-
These are the headers of the request :-
Note: Insomnia also inserts a{ "Content-Type": "application/json", "Authorization": "Bearer <YOUR_ACCESS_TOKEN>" }
User-Agent
header, don’t worry about it, leave it as it is. -
This is the JSON payload I’ll send :-
{ "text": "Excited for my new website? https://rhymbit.com", "poll": { "options": ["Yes", "Definitely", "I 💗 you already!!!"], "duration_minutes": 10 } }
That’s it, hit the Send button and you should see a response with the tweet details.
This is what the response for my request looked like :-
{ "data": { "edit_history_tweet_ids": [ "1766857795331149833" ], "id": "1766857795331149833", "text": "Excited for my new website? https://t.co/e5VZ7DBFmE" } }
and of course, this is the tweet that got posted :-
Deleting a Tweet 💣
All you need is id of the tweet you want to delete.
See that response above, the id is 1766857795331149833
.
We’ll make a DELETE request to this endpoint :-
https://api.twitter.com/2/tweets/:id
So in my case, it would be :-
https://api.twitter.com/2/tweets/1766857795331149833
And that’s it! The tweet is gone! 🤯
You’ll see a response from the API, like so :-
{ "data": { "deleted": true } }
Cheap As$ Twitter 😭
Mr. Elon …, he spent 44 billon dollars on Twitter and then they removed almost all the free features of the API, because they’ve got to make that money back somehow. 😒
Also Twitter API was being abused by bots and spammers, but still … 😒
Currently free tier users can only do the following :-
- Can only post 50 basic tweets a day, and delete 50 tweets a day.
- Can only do basic user lookup
Zoom In & Out using scroll wheel, Reset by double clicking
I swear to god, if one day Twitter is profitable again and they still keep these restrictions, I’mma be so mad. 😡
Conclusion 🎉
The reason I decided to make this series is :-
- I wanted to use the Twitter API for some fun 😏 stuff I was working on.
- Elon fired almost everyone at Twitter. Like the people who replied on forums, the people who wrote the documentation, the people who made the API … they’re all gone. So, it was quite difficult to figure all this out. Lots of trial and error. 🥹
I hope this series helped you out. If you have any questions, feel free to ask me on … well Twitter 😆 or any of my other socials. I’m sure you can find the links to them somewhere on this website. 😅
That’s it!, thanks 🤗 for reading all the way till down here. This series is over, but there’s so much more to explore, so keep digging ⛏️.