Saturday, October 15, 2022

C#: Twitter Volume Stream Coding Challenge (Getting Started cURL/Postman)

Overview

I accepted a C# coding challenge targeting Twitter's Volume Streams API (a RESTful web service). This post demonstrates:

  • Requirements of the coding challenge
  • How to create a Twitter account and a Twitter developer account
    • This includes acquiring a bearer token facilitating access to the Twitter API
  • Using cUrl and Postman (online) to access Twitter's API
Future posts will demonstrate how to implement the coding challenge (C#/.NET 7).

Requirements

The application developed with regard to the coding challenge should generate the following analytics:

  • total number of Twitter tweets
  • top ten hashtags

Setup

The steps to access Twitter's API are well documented but the basic steps are:

  • Create a Twitter account: for readers (like me) who did not have a Twitter account, you can figure it out
  • Create a developer account: Developer Account - Twitter Developer
  • As part of creating a developer account, Twitter also requires an application to be created and for the application Twitter provides:
    • API Key
    • API Key Secret
    • Bearer Token

The speicfic Twitter API V2 to access for this challenge is Volume Streams:


Sample Code

C#

The sample code for the Twitter API v2 is on  GitHub at https://github.com/twitterdev/Twitter-API-v2-sample-code. Cloning the previous git repo reveals zero samples with a CS (C#) file extension. There are Python, Java, and JavaScript samples but not C#.

cURL

To use the Volume Streams API using cURL on Ubuntu first create an environment variable named APP_ACCESS_TOKEN:

export APP_ACCESS_TOKEN=<bearer token provided by Twitter here>

Once the APP_ACCESS_TOKEN environment variable is created, cURL can be used to invoke the Volume Streams API from a terminal window:


When the above cURL command is invoked the output is as follows (a constant stream of 1% of the tweets):

Postman

Before accessing the Twiter Volume Stream endpoint using Postman, create a Postman account. This is required to use Postman for the Web. Legacy Postman was a desktop application but in September 2020 Postman for the Web (open Beta, July 16, 2020) was released for General Availability.

To invoke Twitter's Volume Stream endpoint using Postman, navigate to the "Run in Postman" button and click:


Clicking on Run in Postman invokes the following URL, https://t.co/twitter-api-postman. The previous URL launches Twitter's Public Workspace for Postman: 


Expand the workspaces folders to expose Twitter API V2 / Sample Stream as follows:


The selected item displays GET used to invoke the volume stream endpoint:


Developers who have used Postman will instinctively click on the Authorization tab in order to enter the Twitter-provided bearer token so that the GET can be invoked using the Send button:


Entering the bearer token and clicking on Send will:
  • Require the user to log in to Postman
  • Require the user to create a fork: Twitter's Public Workspace for Postman is a Git repo. Each user runs their own fork and this allows users to contribute to the project. This also isolates a user's instance.
  • Require the user to create a Postman workspace
Once the above tasks are completed the bearer token can be added under the Authorization tab and the endpoint can be invoked using the Send button:

Connection Limits

The developer account type Twitter offers is limited to one connection at a time to the Volume Stream web service. Running the cURL sample above and trying to invoke Postman against the Volume Steam endpoint expectedly generates a 429 error (too many requests):


Appendix A: Coding Challenge




No comments :

Post a Comment