How to Post Tweet with Python and Twitter API
Being lazy is OK, as long as you’re being smart about it. Twitter exposes an API that allows you to execute functionality via an application or script. For example, if you would like to automatically post a set of prewritten tweets daily to your Twitter account automatically, you can use an application that will periodically post to your twitter account without the need of manual intervention. In this example, we will use Python as the primary language that will be talking to the Twitter API.
The steps will be to first login to Twitter, and perform all the account settings needed to enable and obtain API access. Then, we’ll explain how to implement the code and execute a test!
You will need to have a basic understanding of the following
- A Twitter account
- Basic Programming Fundamentals
- Basic Knowledge of Python
- Knowledge of API implementation
- Authenticating via Oauth2
If you do not have programming or web development experience, you can hire us and we can help you with any custom solutions you might need.
Visit our site https://miamiweblab.com or call us 786-228-5224 if you have an idea for a project you would like to get started on.
Twitter Account & Setup
If you have not yet done this, first go to Twitter and create a new account, this should be very simple. Go to www.twitter.com and hit the Signup link on the top right as shown in the screen capture below.
Enable Twitter Development Account
Next, you will need to go to the Developers page on Twitter. You can find that page here https://dev.twitter.com
Once you go to this page, you will need to hit the “Join” button on the top right.
See the screenshot below
After signing up and completing the basic information that is asked, you will be presented with a Thank you page. On the left side navigation, go ahead and click on “My Apps” under Tools and Support section. Or you can follow the link here https://apps.twitter.com/
Create the Application
Now that you are in the “My apps” page, you will need to create a new app. If this is a new account, it will tell you that there are no Apps with a button to create a new one. Click that button to create a new app and fill out the information like the screenshot provided below. You can leave the Callback URL empty for the moment.
(Note If you get an error about a duplicate name, just change the name and retry)
Once you create the application, you will be taken to a dashboard showing your new application details. On the top of this page, click on the Keys and Access Tokens tab
Once you click the Keys and Access Tokens link, take a note of the following data that is shown in the Application Settings section and add it to a file called cfg.py
- Consumer Key (API Key)
- Consumer Secret (API Secret)
- Owner ID
Under this section you will also see a button option to regenerate your Consumer Key and Secret, as well as change your access levels.
Make sure your access levels have write access, you will need this to post tweets!
Create an Access Token
Now you will need to create an Access token. If this is a new account, you should see the following text “You haven’t authorized this application for your own account yet.” and a button underneath that says Create my Access Token. Go ahead and click on that button to get started.
After creating your access token, you should see the following notification message appear on the top of the page that says the following
“Your application access token has been successfully generated. It may take a moment for changes you’ve made to reflect. Refresh if your changes are not yet indicated.”
Now you will see your access token and secret, as well as some buttons underneath to regenerate your access token and/or revoke token access. Take a note of the following under the Your Access Token section and save it to your cfg.py file
- Access Token
- Access Token Secret.
Now is time to start programming and making a basic application that will use your authentication details that were generated on your Twitter account to post a tweet.
- Create a new directory and name it anything you like
- Drop your cfg.py file in the directory created
- change directory to the new directory created: cd twitterdev/
- Run the command touch __init__.py
- Verify your cfg.py file has the Consumer Token, Consumer Token Secret, Access Token, and Access Token Secret.
- Create a new file called post_tweet.py This will be the main file used to talk to Twitter.
- Install the required Twitter libraries. We will be using tweepy and you can install it by running pip install tweepy
Here is a sample of what the cfg.py configuration file should look like
consumer_secret = "xxxxx" consumer_key = "xxxxxx" access_token = "fffffff" access_token_secret = "ggggggggg"
Now you can edit your post_tweet.py file to contain something like the following. You can modify and enhance it later at your own convenience.
import tweepy auth = tweepy.OAuthHandler(consumer_key, consumer_secret) auth.set_access_token(access_token, access_token_secret) api = tweepy.API(auth) api.update_status("Hello this is a test")
With this code, you will now be able to run python post_tweet.py and if no output is returned, that means the script worked successfully.
You can now check your twitter wall and make sure your post worked!