imgApply for Access


Introduction

This article will walk you through the steps to create a simple Twitter App using Python.

Requirements

Prior to creating a Twitter App, you will need the following:

  • Python 3.6 or later installed
  • A Twitter Account

Instructions

The process to create a Twitter App is broken down into the following steps:

      Step 1 - Apply for a Developer Account
      Step 2 - Create an App in Twitter
      Step 3 - Create Keys and Tokens for your App
      Step 4 - Install Python-Twitter Library
      Step 5 - Create a Python Script that Posts to Twitter

Step 1 - Apply for a Developer Account

You need to apply for a developer account through Twitter. To do this, log in to Twitter and visit https://developer.twitter.com/

If this is your first time to log in to the above link, you would see something like this:

imgApply for Access

Click on ‘Apply for a Developer Account’.

imgApplication for Developer Account

Review the instructions on the page. Once ready to proceed, press on ‘Continue’. You will be asked some additional questions in regards to the developer account. You will need to complete ‘Account Details’ and ‘Use case details’.

You will see something like this once you have answered all the questions and agreed to the Terms of Service.

imgConfirmation Screen

Step 2 - Create an App in Twitter

At this point, you’ve signed up for a Twitter Developer Account, confirmed you’re email address and you’re on the ‘Get Started’ Page.

img‘Get Started’ Page

Click on ‘Create an App’ under ‘Get started’. You should see something like this:

imgCreate an app

Click on ‘ Create an app’ again. You will be asked to fill out some information in regards to your app (similar to filling out the developer questionnaire). Here are some of the details you will have to fill out:

imgApp questionnaire

Complete the questionnaire to create your app.

Step 3 - Create Keys and Tokens for your App

Now that you’ve created your Twitter App. You will see something like this:

imgApp details

Click on ‘Keys and tokens’. The Consumer API keys will be listed. You will need also need to ‘Create’ the ‘Access token & access token secret’ for your App.

imgApp Keys and Tokens

Step 4 - Install Python-Twitter Library

This step will walk you through installing the Python-Twitter Library. You will need to download the library from github.com.

Python-Twitter Library Link: https://github.com/bear/python-twitter

imgScreenshot of Python-Twitter github library

Download and extract the library. Run the following command to install the library:

python setup.py install

The first time I tired to install the library, I got the following error message:

error: The ‘requests’ distribution was not found and is required by python-twitter

imgScreenshot of the installation error

Running the install command again resolved the issue.

Step 5 - Create a Python Script that Posts to Twitter

I know it’s been fun, however we’re almost at the end. Now time to write a simple Python program that makes a post to Twitter.

Make sure to import twitter at top of your program:

1
import twitter

Authenticate to Twitter by using your token, token secret, consumer_key and consumer_secret (from Step 3).

1
2
api = twitter.Api(consumer_key=consumer_key, consumer_secret=consumer_secret,
access_token_key=token, access_token_secret=token_secret)

Update your status with the following syntax:

1
status = api.PostUpdate("Hello World #HelloWorld #Python #TwitterApp")

Summary

Voila!! You’ve just sent a tweet!! Go check you’re Twitter!! You’ve made your first Twitter app with Python!!

This article provided the minimum steps required to create a Twitter App. I know what you’re thinking, this is a good start, but running a Python script to send a tweet still sounds like too much work. In a future post I will show you how to use AWS lambda to make this more efficient and self reliant.