skip to Main Content

Hi i’m trying to get all users that follow a specific account on twitter, so I made this code using twitter-api-v2

const followers = await reader.v2.followers(userId)

    let next_token = followers.meta.next_token
    let flist = []

    followers.data.map(e => flist.push(e.username))

    while(next_token !== undefined){

        const more = await reader.v2.followers(userId, { asPaginator: true, pagination_token: next_token })
        next_token =  more?.meta?.next_token

        more.data.data.map(e => flist.push(e.username))
    }

But when I run the code, I get "Too Many Requests", for reaching the twitter followers endpoint rate limit, and idk what to do, is it impossible? i saw many many bots that do that and I just can’t?

2

Answers


  1. You can get this API in v2

    Getting started with the follows lookup endpoints

    GET https://api.twitter.com/2/users/{user-id}/followers
    

    Example

    https://api.twitter.com/2/users/415859364/followers?user.fields=name,username&max_results=3
    

    Result

    $ node get-follower.js
    {
        "data": [
            {
                "id": "1596504879836499971",
                "name": "花花化海",
                "username": "zhanglihang123"
            },
            {
                "id": "1526533712061550595",
                "name": "boy",
                "username": "bernardoy_10"
            },
            {
                "id": "1606507879305187328",
                "name": "Bubsy",
                "username": "BjornBubsy"
            }
        ],
        "meta": {
            "result_count": 3,
            "next_token": "79HP1KIM4TA1GZZZ"
        }
    }
    
    

    enter image description here

    I just 3 followers among 9.6 millions.

    enter image description here

    How to get all?

    That API get maximum 1000 for each API call.
    So first call get 1000 followers next API call with next_token get other 1000 followers, so if want to get 9.6 Millions, you have to call about 9600 API calls.

    This is full code for get 1000 followers.

    const axios = require('axios')
    const config = require('./config.json');
    
    const getAccessToken = async () => {
        try {
            const resp = await axios.post(
                'https://api.twitter.com/oauth2/token',
                '',
                {
                    params: {
                        'grant_type': 'client_credentials'
                    },
                    auth: {
                        username: config.API_KEY,
                        password: config.API_KEY_SECRET
                    }
                }
            );
            return Promise.resolve(resp.data.access_token);
        } catch (err) {
            console.error(err);
            return Promise.reject(err);
        }
    };
    
    const getFollowers = async (token, user_id, max_number) => {
        try {
            const resp = await axios.get(
                `https://api.twitter.com/2/users/${user_id}/followers`,
                {
                    headers: {
                        'Authorization': 'Bearer '+ token,
                    },
                    params: {
                        'user.fields': 'name,username',
                        'max_results': max_number
                    }
                }
            );
            return Promise.resolve(resp.data);
        } catch (err) {
            return Promise.reject(err);
        }
    };
    
    getAccessToken()
        .then((token) => {
            getFollowers(token, '415859364', 1000)
                .then((result) => {
                    console.log(JSON.stringify(result, null, 4));
                })
                .catch(error => console.log(JSON.stringify(error)));
        })
        .catch(error => console.log(JSON.stringify(error)));
    

    Result

    {
        "data": [
            {
                "id": "1606509933230448640",
                "name": "Chelsea Mensah-benjamin",
                "username": "Chelseamensahb"
            },
            {
                "id": "1606508744644251648",
                "name": "Akash Saha",
                "username": "AkashSa98976792"
            },
            {
                "id": "1606339693234204672",
                "name": "L。!!。🏳️‍🌈",
                "username": "LL9777777"
            },
        ...
            {
                "id": "1606362529432997888",
                "name": "Venu Prasanth",
                "username": "prasanthvenu8"
            },
            {
                "id": "1606363199967723523",
                "name": "Heather Bartholomew",
                "username": "HeatherBartho20"
            },
            {
                "id": "1469403002805301248",
                "name": "Gokul Venu",
                "username": "GokulVenu20"
            }
        ],
        "meta": {
            "result_count": 1000,
            "next_token": "0289CA5F0LA1GZZZ"
        }
    }
    
    

    For next 1000 get followers
    This call will be get with pagination_token <- before call’s next_token

    https://api.twitter.com/2/users/415859364/followers?user.fields=name,username&max_results=1000&pagination_token=0289CA5F0LA1GZZZ
    

    Relation between HTTP call with GET parameters and Axios parameters
    It makes how many number of data and each item what kinds of data fields want to get from Tweeter server.

    enter image description here

    If you want to add more user fields, look this URL

    enter image description here

    Login or Signup to reply.
  2. I have a BASIC level API (100$ pm) and not sure if the followers API is accessible to me as well. In their docs they only have POST and DELETE. When I try to call the GET "When authenticating requests to the Twitter API v2 endpoints, you must use keys and tokens from a Twitter developer App that is attached to a Project. You can create a project via the developer portal.","registration_url":"https://developer.twitter.com/en/docs/projects/overview&quot;,"title":"Client Forbidden","required_enrollment":"Appropriate Level of API Access","reason":"client-not-enrolled"

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search