How To: Code Your Own Twitter Client in Python Using OAuth

Code Your Own Twitter Client in Python Using OAuth

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

How to Code Your Own Twitter Client in Python Using OAuth

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.

Requirements

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

  1. You need to install Tweepy from the Git repo.
        git clone https://github.com/tweepy/tweepy
  2. Change to the new Tweepy directory.
        cd tweepy/
  3. Run the setup with the Python installer.
        python setup.py

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

  1. Log in to Twitter.
  2. Go here to register your application and fill in the appropriate data on the form.
  3. Name the application whatever you would like. Save the data.
  4. 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:

How to Code Your Own Twitter Client in Python Using OAuth

  1. Copy the source code here.
  2. 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.
  3. Save it as auth.py.
  4. From a command promt or terminal, type the following command to run the script.
        python auth.py
  5. Follow the URL that the program gives you when you run it and then enter your PIN into the field.
  6. 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:

How to Code Your Own Twitter Client in Python Using OAuth

  1. Get the source code here.
  2. Copy and paste your corresponding keys that we saved in between the quotes.
  3. Save the document as tweet.py.
  4. 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."

How to Code Your Own Twitter Client in Python Using OAuth

So, join in on our coding sessions to learn how to code simple, yet fun programs like this one on your own!

Follow us on Twitter; come chat with us on IRC.

Image via under30ceo

1 Comment

"Download and Install Tweepy

You need to install Tweepy from the Git repo.
git clonehttps://github.com/tweepy/tweepy
Change to the new Tweepy directory.
cd tweepy/
Run the setup with the Python installer.
python setup.py"

I've downloaded the tweepy zip (from the link). How do I do the following steps (the part I made italic) on Windows?

Share Your Thoughts

  • Hot
  • Latest