Skip to content
Advertisement

How do you get the comments of a video using the YouTube Java Client API when given the video ID?

I am looking to write code that takes a video ID as input and retrieves the comments made on the corresponding video. Here’s a link to the API docs. I tried this code

    String videoId = "id";
    YouTube.Comments.List list2 = youtube.comments().list(Arrays.asList("snippet"));
    list2.setId(Arrays.asList(videoId));
    list2.setKey(apiKey);
    Comment c = list2.execute().getItems().get(0);

but I get an IndexOutOfBoundsException on the last line because getItems is returning an empty List. I set videoId as a valid YouTube video ID (one which I have already successfully been able to get video data like views, title, etc from), thinking that would work but clearly I was wrong. Unless I missed something I can’t find anything in the docs for the Video class about getting comment data, so that’s why I’m turning to SO for help again.

EDIT: Per stvar’s comment I tried changing the second line of the above code to

YouTube.CommentThreads.List list2 = youtube.commentThreads().list(Arrays.asList("snippet"));

and of course changed the type of c to CommentThread. It is the CommentThreads API I’m supposed to use, right? Either way, this is also returning an empty list…

Advertisement

Answer

Here is the complete Java code that retrieves all comments (top-level and replies) of any given video:

List<Comment> get_comment_replies(
    YouTube youtube, String apiKey, String commentId)
{
    YouTube.Comments.List request = youtube.comments()
        .list(Arrays.asList("id", "snippet"))
        .setParentId(commentId)
        .setMaxResults(100)
        .setKey(apiKey);
    List<Comment> replies = new ArrayList<Comment>();
    String pageToken = "";

    do {
        CommentListResponse response = request
            .setPageToken(pageToken)
            .execute();

        replies.addAll(response.getItems());

        pageToken = response.getNextPageToken();
    } while (pageToken != null);

    return replies;
}

List<CommentThread> get_video_comments(
    YouTube youtube, String apiKey, String videoId)
{
    YouTube.CommentThreads.List request = youtube.commentThreads()
        .list(Arrays.asList("id", "snippet", "replies"))
        .setVideoId(videoId)
        .setMaxResults(100)
        .setKey(apiKey);
    List<CommentThread> comments = new ArrayList<CommentThread>();
    String pageToken = "";

    do {
        CommentThreadListResponse response = request
            .setPageToken(pageToken)
            .execute();

        for (CommentThread comment : respose.getItems()) {
            CommentThreadReplies replies = comment.getReplies();
            if (replies != null &&
                replies.getComments().size() !=
                comment.getSnippet().getTotalReplyCount())
                replies.setComments(get_comment_replies(
                    youtube, apiKey, comment.getId()));
        }

        comments.addAll(response.getItems());

        pageToken = response.getNextPageToken();
    } while (pageToken != null);

    return comments;
}

You’ll have to invoke get_video_comments, passing to it the ID of the video of your interest. The returned list contains all top-level comments of that video; each top-level comment has its replies property containing all the associated comment replies.

User contributions licensed under: CC BY-SA
7 People found this is helpful
Advertisement