在 Twitter API v2 中获取 extended/full 文本推文
Get extended/full text tweets in Twitter API v2
新的 Twitter v2 API 几周前刚刚发布,所以这可能只是文档尚未完成的问题。
我想做的是在最近的推文中搜索“小狗”和 return 所有带有某种媒体的内容。但是,当我 运行 在 Postman 中进行此搜索时,并非所有 returned 的推文都有 attachments.media_keys
。我注意到那些没有 attachments.media_keys
的是文本以省略号 ...
结尾的推文。我知道在 v1.1 API 中,这个问题是通过在查询参数中指定 tweet_mode=extended
或 tweet.fields=extended_tweet
来解决的。然而,这些在 v2 API 中似乎不起作用,而且我还没有看到任何关于获取推文全文(和相关附件)的文档。有谁知道如何在 v2 中执行此操作?
我的 Postman 查询 url: "https://api.twitter.com/2/tweets/search/recent?query=has:media puppies&tweet.fields=attachments&expansions=attachments.media_keys &media.fields=duration_ms,高度,media_key,preview_image_url,public_metrics,类型,url,宽度
在我的应用程序中,我使用 Node.js Axios 来执行查询:
var axios = require('axios');
var config = {
method: 'get',
url: 'https://api.twitter.com/2/tweets/search/recent?query=has:media puppies&tweet.fields=attachments&expansions=attachments.media_keys&media.fields=duration_ms,height,media_key,preview_image_url,public_metrics,type,url,width',
headers: {
'Authorization': 'Bearer {{my berarer token}}',
}
};
axios(config)
.then(function (response) {
console.log(JSON.stringify(response.data));
})
.catch(function (error) {
console.log(error);
});
问得好,谢谢。我们也是discussing this on the Twitter Developer forums。
在 API 的 v2 中,我们消除了“扩展推文”的概念,因为我们假设所有新应用都理解 280 个字符的概念,因此完整的文本位于推文文本字段中。
你发现的不同之处在于转推或引用的推文中嵌入的文本被截断了。这(也许令人惊讶)与 v1.1 以及以前的高级版和企业版 API 相同。我们正在调查是否要修改它,以及这样做的影响。
我不想以任何方式从 Stack 夺走流量,但您可能会在我们的开发者论坛上找到更多正在进行的更新和信息。谢谢!
截至 2021 年 7 月,这个“问题”或奇怪的行为肯定与转发有关。
为了在获取用户最近的推文的同时获取转推的全文,我使用了以下技巧:
首先,我得到了一个用户最近的推文,关注 docs:
curl "https://api.twitter.com/2/users/2244994945/tweets?expansions=attachments.poll_ids,attachments.media_keys,author_id,entities.mentions.username,geo.place_id,in_reply_to_user_id,referenced_tweets.id,referenced_tweets.id.author_id&tweet.fields=attachments,author_id,context_annotations,conversation_id,created_at,entities,geo,id,in_reply_to_user_id,lang,possibly_sensitive,public_metrics,referenced_tweets,reply_settings,source,text,withheld&user.fields=created_at,description,entities,id,location,name,pinned_tweet_id,profile_image_url,protected,public_metrics,url,username,verified,withheld&place.fields=contained_within,country,country_code,full_name,geo,id,name,place_type&poll.fields=duration_minutes,end_datetime,id,options,voting_status&media.fields=duration_ms,height,media_key,preview_image_url,type,url,width,public_metrics,non_public_metrics,organic_metrics,promoted_metrics&max_results=5" -H "Authorization: Bearer $BEARER_TOKEN"
这是一个全字段查询(并非所有字段都是必需的)但是有必要在返回的 JSON 数据的结构中获取 ['includes']['tweets']
。这是您必须查找转推全文的地方 - 它位于:['includes']['tweets'][0..n]['text]
而所有最近的推文(和转推)都位于 ['data'][0..n]['text']
.
然后你必须将来自 ['data']
的缩短转发与来自 ['includes']['tweets']
的转发相匹配。我使用 ['data'][n]['referenced_tweets'][0]['id']
来完成它,它应该匹配 ['includes']['tweets'][m]['id]
。其中 n
和 m
是一些索引。
为了 100% 安全,您可以检查 ['data'][n]['referenced_tweets'][0]['id']
是否有匹配的 key/value 对:type: retweet
(表明这确实是转推参考),但对我来说 0
索引在所有检查过的情况下都有效,所以不要让事情变得更复杂我现在就这样做了:)
如果这听起来很复杂,只需转储所有已解析的 JSON 和所有推文并检查数据结构。
新的 Twitter v2 API 几周前刚刚发布,所以这可能只是文档尚未完成的问题。
我想做的是在最近的推文中搜索“小狗”和 return 所有带有某种媒体的内容。但是,当我 运行 在 Postman 中进行此搜索时,并非所有 returned 的推文都有 attachments.media_keys
。我注意到那些没有 attachments.media_keys
的是文本以省略号 ...
结尾的推文。我知道在 v1.1 API 中,这个问题是通过在查询参数中指定 tweet_mode=extended
或 tweet.fields=extended_tweet
来解决的。然而,这些在 v2 API 中似乎不起作用,而且我还没有看到任何关于获取推文全文(和相关附件)的文档。有谁知道如何在 v2 中执行此操作?
我的 Postman 查询 url: "https://api.twitter.com/2/tweets/search/recent?query=has:media puppies&tweet.fields=attachments&expansions=attachments.media_keys &media.fields=duration_ms,高度,media_key,preview_image_url,public_metrics,类型,url,宽度
在我的应用程序中,我使用 Node.js Axios 来执行查询:
var axios = require('axios');
var config = {
method: 'get',
url: 'https://api.twitter.com/2/tweets/search/recent?query=has:media puppies&tweet.fields=attachments&expansions=attachments.media_keys&media.fields=duration_ms,height,media_key,preview_image_url,public_metrics,type,url,width',
headers: {
'Authorization': 'Bearer {{my berarer token}}',
}
};
axios(config)
.then(function (response) {
console.log(JSON.stringify(response.data));
})
.catch(function (error) {
console.log(error);
});
问得好,谢谢。我们也是discussing this on the Twitter Developer forums。
在 API 的 v2 中,我们消除了“扩展推文”的概念,因为我们假设所有新应用都理解 280 个字符的概念,因此完整的文本位于推文文本字段中。
你发现的不同之处在于转推或引用的推文中嵌入的文本被截断了。这(也许令人惊讶)与 v1.1 以及以前的高级版和企业版 API 相同。我们正在调查是否要修改它,以及这样做的影响。
我不想以任何方式从 Stack 夺走流量,但您可能会在我们的开发者论坛上找到更多正在进行的更新和信息。谢谢!
截至 2021 年 7 月,这个“问题”或奇怪的行为肯定与转发有关。
为了在获取用户最近的推文的同时获取转推的全文,我使用了以下技巧:
首先,我得到了一个用户最近的推文,关注 docs:
curl "https://api.twitter.com/2/users/2244994945/tweets?expansions=attachments.poll_ids,attachments.media_keys,author_id,entities.mentions.username,geo.place_id,in_reply_to_user_id,referenced_tweets.id,referenced_tweets.id.author_id&tweet.fields=attachments,author_id,context_annotations,conversation_id,created_at,entities,geo,id,in_reply_to_user_id,lang,possibly_sensitive,public_metrics,referenced_tweets,reply_settings,source,text,withheld&user.fields=created_at,description,entities,id,location,name,pinned_tweet_id,profile_image_url,protected,public_metrics,url,username,verified,withheld&place.fields=contained_within,country,country_code,full_name,geo,id,name,place_type&poll.fields=duration_minutes,end_datetime,id,options,voting_status&media.fields=duration_ms,height,media_key,preview_image_url,type,url,width,public_metrics,non_public_metrics,organic_metrics,promoted_metrics&max_results=5" -H "Authorization: Bearer $BEARER_TOKEN"
这是一个全字段查询(并非所有字段都是必需的)但是有必要在返回的 JSON 数据的结构中获取 ['includes']['tweets']
。这是您必须查找转推全文的地方 - 它位于:['includes']['tweets'][0..n]['text]
而所有最近的推文(和转推)都位于 ['data'][0..n]['text']
.
然后你必须将来自 ['data']
的缩短转发与来自 ['includes']['tweets']
的转发相匹配。我使用 ['data'][n]['referenced_tweets'][0]['id']
来完成它,它应该匹配 ['includes']['tweets'][m]['id]
。其中 n
和 m
是一些索引。
为了 100% 安全,您可以检查 ['data'][n]['referenced_tweets'][0]['id']
是否有匹配的 key/value 对:type: retweet
(表明这确实是转推参考),但对我来说 0
索引在所有检查过的情况下都有效,所以不要让事情变得更复杂我现在就这样做了:)
如果这听起来很复杂,只需转储所有已解析的 JSON 和所有推文并检查数据结构。