Search Followers of a Twitter User Using v2 API

Updated: Sep 20, 2023

In this example, you are going to see how to search and retrieve the followers of a Twitter user. Accessing follower data enables you to gather insights about the audience and engagement of your Twitter account. 

The ability to search and retrieve followers of a Twitter user is valuable for social media analytics. You can analyze the demographics, interests, and engagement levels of your followers to gain insights into the audience and optimize your social media strategies accordingly.

 

Java Code Listing

package com.northconcepts.datapipeline.examples.twitter2;

import com.northconcepts.datapipeline.core.DataReader;
import com.northconcepts.datapipeline.core.DataWriter;
import com.northconcepts.datapipeline.core.StreamWriter;
import com.northconcepts.datapipeline.job.Job;
import com.northconcepts.datapipeline.twitter2.TwitterFollowerReader;


public class SearchFollowersOfATwitterUserUsingV2 {

    private static final String API_KEY = "YOUR_API_KEY";
    private static final String API_SECRET = "YOUR_API_SECRET";
    private static final String ACCESS_TOKEN = "YOUR_ACCESS_TOKEN";
    private static final String ACCESS_TOKEN_SECRET = "YOUR_ACCESS_TOKEN_SECRET";

    private static final String USERNAME = "TwitterDev";

    public static void main(String[] args) {
        DataReader reader = new TwitterFollowerReader(API_KEY, API_SECRET, ACCESS_TOKEN, ACCESS_TOKEN_SECRET, USERNAME);
        DataWriter writer = new StreamWriter(System.out);

        Job.run(reader, writer);
    }
}

 

Code Walkthrough

  1. First, access parameters to connect with Twitter v2 API are specified. They include the API key, API secret, access token and its secret, and account username.
  2. TwitterFollowerReader is created with the above attributes to get followers of a Twitter user.
  3. Data are transferred from TwitterFollowerReader to the console via Job.run() method. See how to compile and run data pipeline jobs.

TwitterFollowerReader

Obtains records of Twitter users that follow the specified Twitter account using Twitter v2 API. It extends AbstractTwitterReader class and its constructor takes API_Key, API_Secrete, Access Token, Access_Token Secrete and Username of the account whose followers will be obtained. You can optionally use Bearer_Token instead of Access_Token and Access_Token_Secrete.

 

Output

Followers of the specified user name are printed on the console.

Mobile Analytics