1

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?

JayJay
  • 17
  • 6

1 Answers1

2

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

Bench Vue
  • 5,257
  • 2
  • 10
  • 14
  • lol thanks, I wasn't understanding how to pass headers on axios too and I tought the limit of ppl requested was 100 – JayJay Dec 24 '22 at 12:16
  • @JayJay, I updated my answer for explaining axios's parameter parts, let me know if you still don't understand it. – Bench Vue Dec 24 '22 at 12:43
  • 1
    Hi, i was trying to use the lib twitter-api-v2 and this explanation is toooo good and so helpful, thank you, helped me a lot and now I really know what i'm doing – JayJay Dec 25 '22 at 19:11
  • @JayJay I am happy to hear you got it. I am also learn a lot from this topic, thanks you. – Bench Vue Dec 25 '22 at 19:24
  • @BenchVue, I am trying to get follower list of Twitter user, can you please guide me? Here is the question link: https://stackoverflow.com/questions/76726633/get-twitter-account-followers-using-api-and-google-apps-script – EagleEye Jul 20 '23 at 15:27
  • @EagleEye, Sorry I am using free account of Twitter, I can't test API anymore. I can't help you. – Bench Vue Jul 20 '23 at 15:41
  • Free account also provide API keys, I have them – EagleEye Jul 20 '23 at 15:54
  • Free account can do only three APIs. `POST /2/tweets`, `DELETE /2/tweets/:id` and `GET /2/users/me`. That it. Can't do `GET /2/users/${username}/followers` API as same as your question. – Bench Vue Jul 20 '23 at 16:32
  • Was this endpoint depreciated? It's no more in twitter docs: https://stackoverflow.com/questions/74894975/how-to-get-all-followers-from-a-specific-user-using-twitter-api-v2 – MantasFam Aug 21 '23 at 12:29
  • @MantasFam, That link was removed by Tweet. I can't test no more Tweet API due to my free account not allow to call API any more. But Follows API still there, you can figure out from [this](https://developer.twitter.com/en/docs/twitter-api/users/follows/introduction) if you are a paid user. – Bench Vue Aug 21 '23 at 20:55