Dart 中的 Twitter API 2.0 Conversation_id

Twitter API 2.0 Conversation_id in Dart

我正在尝试使用 Twitter 2.0 API 执行一个 http get 请求到 return 使用 conversation_id 请求的对话。

它仅 return 发送主要推文,而不 return 发送对推文的回复。谁能告诉我我做错了什么?任何帮助将不胜感激。

import 'package:http/http.dart';
import 'dart:convert';


class TwitterService{
  final String twitterUrl =
      'https://api.twitter.com/2/tweets?ids=1176434159520075777&tweet.fields=author_id,conversation_id,created_at,in_reply_to_user_id,referenced_tweets&expansions=author_id,in_reply_to_user_id,referenced_tweets.id&user.fields=name,username'
  ;

    getPosts() async{
    Response response = await get(Uri.parse(twitterUrl),
      headers: {
        'Authorization': 'Bearer $BEARERTOKEN',
      }
      );
    if(response.statusCode == 200) {
      String tweetList = response.body;
      var collection = json.decode(tweetList);
      print(collection);
      return collection;

    } else {
      print('error');
    }
  }

}

我无法专门在 Dart 方面提供帮助(这是我要学习的东西清单上的),但从 Twitter API 的角度来看,您所做的调用将使您能够conversation_id,但不是对话的其余部分。

如果您查看 Twitter API documentation,您会看到对 /2/tweets 端点的请求会让您获得对话的 ID。

To request the conversation_id for all Tweets returned on a v2 endpoint, the tweet.fields=conversation_id field can be added to the request parameters. The conversation_id field is always the Tweet ID of the original Tweet in the conversation reply thread. All Tweets within the same reply thread, including reply threads that are created from earlier reply threads, will show the same conversation_id.

您缺少的步骤是下一阶段,即使用搜索 API 检索属于同一线程的推文:

The conversation_id can be used as a search query parameter when using either recent search or as an operator within a rule for filtered stream. Using the operator on its own will result in the entire conversation thread of Tweets being returned in either real time through filtered stream, or paginated in reverse chronological order from recent search.

您应该能够适应 Dart 的示例:

curl --request GET \
  --url 'https://api.twitter.com/2/tweets/search/recent?query=conversation_id:1279940000004973111&tweet.fields=in_reply_to_user_id,author_id,created_at,conversation_id' \
  --header 'Authorization: Bearer $BEARER_TOKEN' 

另请注意,您受最近搜索时间段的限制,即 7 天的历史记录。