What will we cover
- All the steps from creating a twitter account to implement a working Python program that will post a message in that account.
To achieve that you need the to go through the following steps.
- Create a Twitter Account, if you don’t already have one.
- Apply for a developer account, otherwise you will not be able to use the Twitter API
- Create an App in Twitter, which is used to generate the tokens you need to connect your program.
- Generate the needed tokens.
- Install Tweepy, which is a Python library that makes all the API stuff easy for you.
- The actual Python program.
Well, let’s get ready.
Step 1: A Twitter Account
Okay, this one is simple. You need to have a Twitter account to tweet from. If you do not already have one, then go to twitter.com and create an account.
Step 2: Apply for a Developer Account
Yes, you need to apply for a developer account. Don’t worry it is an automated process and if you follow the instructions here, you should get it immediately.
Log in to developer.twitter.com with your twitter credentials (if it does not happen automatically).
See in the top right corner it says Apply. Press that and continue to this screen.
Press the “Apply for a developer account”. Then you end up here.
Choose under Hobbyist “Making a bot” and click Next.
That will guide you to a verification page. You might need to enter your phone number, your country and what to call you (your name).
You will be promoted by questions that you need to fill out.
And then press next. Verify the data you entered and press “Verify”. Then finally you need to submit the application.
Then you should receive an email with confirmation.
Step 3: Create an App
From the link in the confirmation mail you get directed to here.
Where you should find the menu with Apps and click it.
Then create an App. That is, you need to click “Create an app” in the upper right corner.
You should obviously enter you own URL.
Finally enter something similar and press create.
Step 4: Create Access tokens and get API tokens
To do that you need to click Keys and Tokens.
On that page you will get your API key and API secret key (not included in picture, but you should find them there).
Next is to generate Access token and Access token secret key. Press “Generate” and copy them. You will need them.
Step 5: Install Tweepy
Tweepy is an easy to use library to Python that will make your life easy. In a terminal type.
pip install tweepy
You might be using pip3 and you might need to be admin or install it locally.
Step 6: Implement your first Python Bot
Now to the fun stuff.
import tweepy # You need to replace all these with your tokens. consumer_key = "Replace this with your API token here" consumer_secret = "Replace this with your API secret token here" access_token = "Replace this with your Access token" access_token_secret = "Replace this with your Access secret token" auth = tweepy.OAuthHandler(consumer_key, consumer_secret) auth.set_access_token(access_token, access_token_secret) api = tweepy.API(auth) # Now this is actually doing what we want. api.update_status(status="Hello, World!")