我正在关注此 twitter api 文档 来获取推文的详细信息,但我似乎无法使用此 API 获取完整的推文详细信息(转发计数、心形、地理标签等)。我仅从 API 中收到一条推文的响应:
{
edit_history_tweet_ids: [ '1766576440005333244' ],
id: '1766576440005333244',
text: 'RT @MOdesign19: Congratulations KTR. #saloneX #supportlocalfootball'
}
我已经输入了正确的查询参数(如
expansions
、tweet.fields
),但似乎没有任何东西能让我获得该推文的详细报告。这就是我的代码的样子:
const needle = require("needle");
const YOUR_BEARER_TOKEN = "MY-TOKEN-HERE";
const TWEET_ID = "1766576440005333244"; // Replace with the actual tweet ID
// Define the endpoint URL
const endpointUrl = `https://api.twitter.com/2/tweets/${TWEET_ID}`;
// Set up the request headers
const headers = {
Authorization: `Bearer ${YOUR_BEARER_TOKEN}`,
"Content-Type": "application/json",
};
// Specify the fields you want to retrieve
const tweetFields =
"attachments,author_id,context_annotations,conversation_id,created_at,entities,geo,id,in_reply_to_user_id,lang,non_public_metrics,public_metrics,organic_metrics,promoted_metrics,possibly_sensitive,referenced_tweets,reply_settings,source,text,withheld";
// Set up the request parameters including the desired fields
const params = {
"tweet.fields":
"attachments,author_id,context_annotations,conversation_id,created_at,entities,geo,id,in_reply_to_user_id,lang,public_metrics,referenced_tweets,reply_settings,source,text,withheld",
expansions: "author_id",
"user.fields":
"created_at,description,entities,id,location,name,pinned_tweet_id,profile_image_url,protected,public_metrics,url,username,verified,verified_type,withheld",
"media.fields":
"duration_ms,height,media_key,preview_image_url,type,url,width,public_metrics,alt_text,variants",
"place.fields":
"contained_within,country,country_code,full_name,geo,id,name,place_type",
"poll.fields": "duration_minutes,end_datetime,id,options,voting_status",
};
async function helloWorld() {
// Make the GET request
const response = await needle("get", endpointUrl, params, { headers });
console.log(response.body.data);
}
helloWorld();
由于您正在执行单条推文查找,因此您可以向该端点添加额外的参数以获取特定于位置的详细信息,如下所示:
https://api.twitter.com/2/tweets/:id?expansions=author_id,geo.place_id&user.fields=created_at,description,entities,location
将
:id
替换为您的目标推文 ID。您将得到如下回复(我已从以下回复中删除了个人详细信息,但结构相似):
{
"data": {
"id": "12345",
"text": "Post Content comes here",
"author_id": "987654",
"edit_history_tweet_ids": [
"098765"
]
},
"includes": {
"users": [
{
"username": "johndoe",
"entities": {
"description": {
"hashtags": [
{
"start": 70,
"end": 90,
"tag": "anyusedtagnamegoeshere"
}
]
}
},
"description": "bio description of the author goes here",
"name": "author name",
"id": "123456xxxx",
"location": "Uganda",
"created_at": "2022-09-22T07:15:46.000Z"
}
]
}
}
要了解有关这些扩展的更多信息并在您的回复中添加更多额外信息您可以查看文档
要获取有关特定推文的总喜欢和转发的信息,我知道有两个不同的端点。
https://api.twitter.com/2/tweets/:id/liking_users
https://api.twitter.com/2/tweets/:id/retweeted_by
如果您仍然没有像我一样获得所需的相关数据。尝试用本机 fetch api 替换 Needle 库并使用 oauth 1.0 身份验证,而不是使用不记名令牌。