在 Twitter API v2 中获取扩展/全文推文

mee*_*lla 6 twitter node.js postman

新的 Twitter v2 API 几周前刚刚发布,因此这可能只是文档尚未完成的问题。

我想做的是在最近的推文中搜索“小狗”,并返回所有附加了某种媒体的推文。但是,当我在 Postman 中运行此搜索时,并非所有返回的推文都有attachments.media_keys. 我注意到没有的attachments.media_keys是文本以省略号结尾的推文...。据我所知,在 v1.1 API 中,这个问题是通过tweet_mode=extended在查询参数或 中指定来解决的tweet.fields=extended_tweet。然而,这些似乎在 v2 API 中不起作用,而且我还没有看到任何有关获取推文全文(以及相关附件)的文档。有谁知道如何在 v2 中做到这一点?

我的邮递员查询网址:“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 ,类型,网址,宽度”

在我的应用程序中,我使用 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);
});
Run Code Online (Sandbox Code Playgroud)

Pic*_*ard 8

截至 2021 年 7 月,这个“问题”或奇怪的行为肯定与转发有关。

为了在获取用户最近的推文的同时获取转发的全文,我做了以下技巧:

首先,我收到以下文档的用户的最新推文:

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"
Run Code Online (Sandbox Code Playgroud)

这是一个全字段查询(并非所有字段都是必需的),但有必要了解['includes']['tweets']返回的 JSON 数据的结构。您必须在此处查找转发的全文 - 它位于:['includes']['tweets'][0..n]['text]而所有最近的推文(和转发)都可以在 中找到['data'][0..n]['text']

然后,您必须将来自 的缩短的转发['data']与来自 的转发相匹配['includes']['tweets']。我使用['data'][n]['referenced_tweets'][0]['id']which should match来做到这一点['includes']['tweets'][m]['id]。其中nm是一些索引。

为了100%安全,你可以检查是否['data'][n]['referenced_tweets'][0]['id']有匹配的键/值对:(type: retweet表明这实际上是一个转发参考),但对我来说,索引0在所有检查的情况下都有效,所以不要让事情变得更加复杂,我这样保留它现在 :)

如果这听起来很复杂,只需转储整个解析的 JSON 和所有推文并检查数据的结构。


And*_*per 7

很好的问题,谢谢。我们\xe2\x80\x99也在Twitter 开发者论坛上讨论这个问题。

\n

在 API v2 中,我们消除了 \xe2\x80\x9c 扩展 Tweet\xe2\x80\x9d 的概念,因为我们假设所有新应用程序都理解 280 个字符的概念,因此完整文本位于 Tweet 文本字段中。

\n

您\xe2\x80\x99 发现的差异在于转发或引用的推文中嵌入的文本被截断。这与 v1.1 以及以前的高级和企业 API 相同(也许令人惊讶)。我们正在调查是否要修改这一点,以及这样做的影响。

\n

我不\xe2\x80\x99t以任何方式想要从Stack中夺走流量,但您可能会在我们的开发者论坛上找到更多正在进行的更新和信息。谢谢!

\n