Priyamal Priyamal - 1 month ago 19
Java Question

how to retrieve comments of a youtube video

I am trying to get all the comments on a youtube video using following approach, but i cant fetch all the comments related to the video only some of the comments are fetched. i tried googling this but nothing helped, can someone explain , how to get all the comments related to a youtube video.

//url :- "https://www.googleapis.com/auth/youtube.force-ssl"

public void ReadAllComments(String videoId){
try {

CommentThreadListResponse videoCommentsListResponse = youtube.commentThreads()
.list("snippet").setVideoId(videoId).setTextFormat("plainText").execute();

List<CommentThread> videoComments = videoCommentsListResponse.getItems();

for (CommentThread videoComment : videoComments) {
CommentSnippet snippet = videoComment.getSnippet().getTopLevelComment().getSnippet();
System.out.println(" - Author: " + snippet.getAuthorDisplayName());
System.out.println(" - Comment: " + snippet.getTextDisplay());
System.out.println("\n-------------------------------------------------------------\n");
}

} catch (Exception e) {
e.printStackTrace();
}

}

Answer Source
  CommentThreadListResponse videoCommentsListResponse = youtube.commentThreads()
.list("snippet").setVideoId(videoId).setTextFormat("plainText").execute();

changing above code to this worked,

 CommentThreadListResponse videoCommentsListResponse = youtube.commentThreads()
.list("snippet").setVideoId(videoId).setMaxResults(50l).setTextFormat("plainText").execute();