【问题标题】:Get extended/full text tweets in Twitter API v2在 Twitter API v2 中获取扩展/全文推文
【发布时间】:2020-12-11 20:54:51
【问题描述】:

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

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

我的 Postman 查询网址:“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"

在我的应用中,我使用 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);
});

【问题讨论】:

    标签: node.js twitter postman


    【解决方案1】:

    很好的问题,谢谢。我们也是discussing this on the Twitter Developer forums

    在 API 的 v2 中,我们已经消除了“扩展推文”的概念,因为我们假设所有新应用都理解 280 个字符的概念,因此完整的文本位于推文文本字段中。

    您发现的不同之处在于转推或引用的推文,其中嵌入的文本被截断。这(可能令人惊讶)与 v1.1 以及以前的高级和企业 API 相同。我们正在调查是否要对此进行修改,以及这样做的影响。

    我无意从 Stack 中夺走流量,但您可能会在我们的开发者论坛上找到更多持续更新和信息。谢谢!

    【讨论】:

      【解决方案2】:

      截至 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'] 的转推相匹配。我使用应该匹配['includes']['tweets'][m]['id]['data'][n]['referenced_tweets'][0]['id'] 来做到这一点。其中nm 是一些索引。

      为了 100% 安全,您可以检查 ['data'][n]['referenced_tweets'][0]['id'] 是否有匹配的键/值对:type: retweet(表明这确实是一个转发参考),但对我来说,0 索引适用于所有检查的情况所以为了不让事情更复杂,我现在就这样了:)

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

      【讨论】:

        猜你喜欢
        • 2022-01-05
        • 2022-12-22
        • 2022-10-05
        • 2023-01-21
        • 1970-01-01
        • 2021-12-13
        • 2015-08-27
        • 2012-12-17
        • 2016-06-21
        相关资源
        最近更新 更多