Search Followers of a Twitter User Using v2 API

Updated: Jun 4, 2023

In this example you are going to see how to search followers of a Twitter user using Data Pipeline's TwitterFollowerReader. TwitterFollowerReader uses Twitter v2 API to obtain records from Twitter.

Java Code listing

/*
 * Copyright (c) 2006-2022 North Concepts Inc.  All rights reserved.
 * Proprietary and Confidential.  Use is subject to license terms.
 * 
 * https://northconcepts.com/data-pipeline/licensing/
 */
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, parameters which are going to be used to connect to Twitter v2 API are specified.
  2. Username of the twitter user is stored in USERNAME field.
  3. TwitterFollowerReader is created to search followers of any Twitter user.
  4. 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 it's 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 printed on the console.

Mobile Analytics