简体   繁体   English

如何使用 twitter-api-v2 从特定用户获取所有关注者

[英]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嗨,我正在尝试让所有在 Twitter 上关注特定帐户的用户,所以我使用 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?但是当我运行代码时,我收到“太多请求”,因为达到了 Twitter 关注者端点速率限制,我不知道该怎么办,这是不可能的吗? i saw many many bots that do that and I just can't?我看到很多机器人都这样做,但我做不到?

You can get this API in v2你可以在 v2 中获得这个 API

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 .9.6 millions粉丝中,我只有 3 个粉丝。

在此处输入图像描述

How to get all?如何获得所有?

That API get maximum 1000 for each API call.对于每个 API 调用,该 API 最多获得 1000 个。 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.因此,首先使用next_token调用 get 1000 followers next API 调用获取其他 1000 followers,因此如果想要获得 960 万,则必须调用大约 9600 API 调用。

This is full code for get 1000 followers.这是获得 1000 个关注者的完整代码。

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对于接下来的 1000 个获取关注者此调用将使用pagination_token <- 在调用的next_token之前获取

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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