oauth2.0-bot icon indicating copy to clipboard operation
oauth2.0-bot copied to clipboard

Sample code for creating a bot with OAuth 2.0 Authorization Code Flow with PKCE and V2 of the Twitter API.

Creating a Twitter bot with OAuth 2.0 and v2 of the Twitter API

Bots on Twitter are what makes the conversation on Twitter so lively. There are many unique bots on Twitter, from tiny aquariums to warning you of sewer overflow in NYC. Bots make Twitter unique and engaging.

When our team launched the manage Tweets endpoints, I built a bot @FactualCat that Tweeted cat facts daily. This bot used OAuth 1.0a to authenticate, but I wanted to challenge myself to create a bot that used OAuth 2.0 Authorization Code Flow with PKCE.

Since I was used to the OAuth 1.0a authentication flow, I had a hard time figuring out exactly what I needed to do to authenticate on behalf of my bot and get it running so that it could make requests regularly.

This code sample will allows you to make a bot similar to @Factual__Dog, a bot that Tweets dogs facts twice daily. By the end of this tutorial, you should be able to create a Twitter bot that utilizes OAuth 2.0 Authorization Code Flow with PKCE for authentication.

Prerequisites

Tutorial

To learn more about this code sample, check out our tutorial which walks you through the steps to create this bot.