Twitter API 2.0 Conversation_id in Dart

496

I'm not able to help on the Dart side specifically (that's on my list of things to learn), but from a Twitter API perspective, the call you've made will enable you to get the conversation_id, but not the rest of the conversation.

If you look at the Twitter API documentation you'll see that the request to the /2/tweets endpoint will let you get the ID for the conversation.

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.

The step you are missing is the next stage, which is to use the search API to retrieve the Tweets that belong to the same thread:

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.

Example that you should be able to adapt for 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' 

Also note that you're bounded by the recent search time period, which is 7 days of history.

Share:
496
mlost
Author by

mlost

Updated on December 29, 2022

Comments

  • mlost
    mlost over 1 year

    I am attempting to perform an http get request with the Twitter 2.0 API to return a conversation using the conversation_id request.

    It is returning only the main tweet, but is not returning the replies to the tweet. Can anyone tell me what I am doing incorrectly? Any help would be greatly appreciated.

    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');
        }
      }
    
    }
    
  • mlost
    mlost about 3 years
    Thank you for the response and I will mark this as solved. I will work on applying for an academic account to get the full conversation