This is my attempt to show people some cool things we can do with programming, and to give a small incentive for people to join the weekly Community Bytes. I wanted to make a tutorial on how to use Twitter from the command line with Python. This would have been an easy project to do not too far back, and a good bit of practice for a beginner to programming. It teaches that you can do useful stuff with programming. However, a while ago Twitter started using something called OAuth to access the Twitter API.
OAuth was implemented for added security. Before, anyone could code a program to use Twitter and make it store the Twitter ID and password. Twitter thought it would be a good idea to add OAuth, which creates public keys, much like SSH, to exchange with the Twitter API so the application is unable to access your password, mitigating the chances of a program stealing user credentials.
Anyhow, let's get started.
Step 1 Download a Twitter Library
Text in bold is a terminal command for Linux. Windows users, simply download Tweepy straight from the repository.
To make this as simple as possible, we are going to use a pre-made library that we can call functions from. For this tutorial, I am going to use the Tweepy library. Recoding a Twitter API library from scratch is beyond the scope of this tutorial.
Download and Install Tweepy
- You need to install Tweepy from the Git repo.
git clone https://github.com/tweepy/tweepy
- Change to the new Tweepy directory.
- Run the setup with the Python installer.
Step 2 Register Our Application with Twitter
We need to let Twitter know that we are allowing our application to have access to our Twitter account. This will also give us the keys we need.
Register our new app
- Log in to Twitter.
- Go here to register your application and fill in the appropriate data on the form.
- Name the application whatever you would like. Save the data.
- Leave the browser open with our registry data, we need to create our application now.
Exchange the Unique Keys
We need to create a small script that exchanges the keys before we code the actual program. This is done separately because it only needs to be used once, therefore, it would take up space in the finished code. Here is a picture of mine without the keys:
- Copy the source code here.
- Paste the source code in a text document, abd paste your keys in the appropriate field by copying them from your Twitter page that we left open earlier.
- Save it as auth.py.
- From a command promt or terminal, type the following command to run the script.
- Follow the URL that the program gives you when you run it and then enter your PIN into the field.
- Copy and paste the new keys that it give you into a text document for later use.
Step 3 Create the Command Line Application
We need to take our keys that we saved from the last part and paste them into our bit of Python code. This will ensure that Twitter knows it is an application we have authorized tweeting as us, but we don't have to risk exposing our password to the program (in case it was a 3rd party program).
Paste the Keys in Our program
We need to paste all of the corresponding keys in our program. Here is a picture of how it came out:
- Get the source code here.
- Copy and paste your corresponding keys that we saved in between the quotes.
- Save the document as tweet.py.
- To use the program, execute the following code from the command line.
python tweet.py "This is a tweet using Null Byte's tweet.py script."
So, join in on our coding sessions to learn how to code simple, yet fun programs like this one on your own!
Want to start making money as a white hat hacker? Jump-start your white-hat hacking career with our 2020 Premium Ethical Hacking Certification Training Bundle from the new Null Byte Shop and get over 60 hours of training from ethical hacking professionals.