简体   繁体   中英

How to get all followers from a specific user using twitter-api-v2

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?

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"
    }
}

在此处输入图像描述

I just 3 followers among 9.6 millions .

在此处输入图像描述

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM