Twitter v2 API 搜索端点 "Field Authorization Error"

Twitter v2 API search endpoint "Field Authorization Error"

在 Twitter v2 API 搜索端点中请求 'organic_metrics' 字段时,我收到以下信息:“字段授权错误”

这是我在 Nodejs 中的请求脚本

function requestSearch (keyword) {

    const search = {
        method: 'GET',
        uri: 'https://api.twitter.com/2/tweets/search/recent',
        headers: {
            "User-Agent": "v2RecentSearchJS",
            "authorization": `Bearer ${process.env.TWITTER_BEARER_TOKEN}`
        },
        qs: {
            query: "context:66.1001503516555337728 context:66.857879456773357569" // `entity: ${keyword}`
        },
        json: true,
        gzip: true
    };

    const params = {
        "ids": "1397885797957738496", // Edit Tweet IDs to look up
        "tweet.fields": "context_annotations,entities,organic_metrics", // Edit optional query parameters here
        "user.fields": "created_at" // Edit optional query parameters here,
    }
    const lookup = {
        method: 'GET',
        uri: 'https://api.twitter.com/2/tweets',
        headers: {
            "User-Agent": "v2RecentSearchJS",
            "authorization": `Bearer ${process.env.TWITTER_BEARER_TOKEN}`
        },
        qs: params
    }
    return rp(lookup)
        .then(response => {
            // console.log('API call response:', response);
            return response
        })
        .catch((err) => {
            console.log('API call error:', err.message);
        });
}

根据 Twitter documentation:

Public metrics can be requested with OAuth 2.0 Bearer Token authentication. Non-public metrics can be requested for owned/authorized Tweets only. This means developers are required to authenticate using OAuth 1.0a User Context authorization. If you need to generate an access token and secret for the Tweet owner, you can do so with the 3-legged OAuth process.

[...]

Organic metrics: Grouping of public and non-public metrics attributed to an organic context (posted and viewed in a regular manner). Requires OAuth 1.0a User Context authentication.

在这种情况下,您正在使用 Bearer Token 访问 API,因此您将无法访问有机指标(这就是“字段授权错误”消息告诉您的内容) .您可以改用 public_metrics 字段。