Automate Tweets with Python3

 

The greatest thing about social media is the ability to market yourself as a technology professional across the globe. Anywhere from the country you live into a country you know nothing about. When you want to market yourself, you may be thinking oh no, I need to log into Twitter and tweet out manually. With Python, that manual process goes away!

In this blog post, you’re going to learn about a Python library called tweepy.

Installing Tweepy

The installation process for Tweepy is much like any other Python library. Simply install it with pip, the Python package manager.

Applying For a Twitter Developer Account

Once you have Tweepy installed, you can write Python code to interact with your Twitter account. However, before you do that, you’ll need a Twitter Developer Account.

There is an apply process that you’ll need to do. Luckily, you don’t have to pay for anything. You simply just apply, answer a few questions, and almost immediately you should have a Twitter Developer Account.

Go to the URL here to apply.

Setting Up A Twitter Developer Project

Now that you have the Twitter Developer Account set up, you’ll need to create a new project.

First, go to https://www.developer.twitter.com and log in with your Twitter account. You should see a home page similar to the screenshot below.

Under the overview page, click on the blue + Add App button.

Choose a name for the app and click the blue Complete button.

You will now be presented with a few keys, which you’ll need for the next section.

Writing Python Code for Tweepy

Now it’s time to automate Twitter Tweets with Python

Open up your favorite IDE or text editor to start writing some code to interact with Twitter at the API level. For the purposes of this blog post, you can use VS Code.

Create a new Python file and name it tweepy.py

The first line of code should be the import of tweepy:

import tweepy

Next, add in the authentication variables. Only do this for testing purposes only! Never add in API keys and token to source control.

api_key = ''
api_secret = ''
access_token = ''
access_token_secret = ''

After you add in the variables for the keys/secrets, you’ll need to authenticate to Twitter with the OAuthHandler and access tokens. There will be two functions you’ll use from Tweepy:

  • OAuthHandler()
  • set_access_token()

The OAuthHandler() function takes two arguments, the API key and the API secret. The set_access_token() function takes two arguments, the access token and the access token secret.

auth = tweepy.OAuthHandler(api_key, api_secret)
auth.set_access_token(access_token, access_token_secret)

Next, you can use Tweepy’s built-in error handling for authentication issues.

try:
    api = tweepy.API(auth)

except tweepy.TweepError:
    print('Error! Failed to authenticate')

Finally, it’s time to use the update_status() function to send out a new Tweet.

api.update_status("Posting from Python")

All together, the code should look like the below:

import tweepy

# Auth keys. Only put API and tokens in your script for testing only!!!
# Never commit to a public repository.
api_key = ''
api_secret = ''
access_token = ''
access_token_secret = ''

# Authenticate via the API key/secret and the access token/secret
auth = tweepy.OAuthHandler(api_key, api_secret)
auth.set_access_token(access_token, access_token_secret)

# Error handling for authentication errors
try:
    api = tweepy.API(auth)

except tweepy.TweepError:
    print('Error! Failed to authenticate')

# Create a new tweet
api.update_status("Posting from Python")

You can find the code base on GitHub here

Now that the code is written, it’s time to run it! Run the tweepy.py script and then check your tweets on Twitter. You should see the Posting from Python tweet.

Congrats! You have successfully used Python to automate posting to Twitter.

Leave a Reply

Your email address will not be published. Required fields are marked *