CommentThread
Learn how to retrieve comment threads and add a top-level comment.
We'll cover the following
Users can post comments on videos, and other users can reply to comments, creating threads. The information regarding these comments and threads is stored in the commentThread
resource.
Note: The top-level comment and replies are actually
comment
resources nested inside thecommentThread
resource.
Supported methods
The API supports the following methods for the commentThread
resource:
list
: Returns a list of comment threads associated with a video.insert
: Creates a new top-level comment.
Let’s look at each method in detail with the help of a few examples.
The list
method
The list
method can retrieve all comment threads associated with a video or about a specified channel. It uses the videoId
and channelId
parameters to identify the video and channel, respectively.
Let’s look at an example of using the videoId
parameter to get comment threads associated with it. In the code below, add a video ID to the VIDEO_ID
field.
api_service_name = "youtube"api_version = "v3"DEVELOPER_KEY = "{{API_KEY}}"youtube = googleapiclient.discovery.build(api_service_name, api_version, developerKey = DEVELOPER_KEY)request = youtube.commentThreads().list(part="snippet,replies",videoId="{{VIDEO_ID}}")response = request.execute()
Note: We can retrieve the comment threads for a channel by using the
channelId
parameter in place ofvideoId
.
In the JSON response received, we can see a list of objects named items
. Each object in this list is a commentThread
resource, which we can verify by looking at the value of the kind key, which is youtube#commentThread
. You’ll also find the nested objects for the snippet
and replies
parts for each commentThread
resource.
The insert
method
The insert
method adds a new top-level comment to a video or channel. This example will create a top-level comment on a video. The request body should have the snippet
part, where you can set the metadata of a comment, such as the comment’s text and the video ID of the video we want to comment on.
Note: On line 20 in the code below, you need to add a video ID on which you want to add a comment. You can also change the default content of the comment on line 23 with something of your choice.
Also, to add a top-level comment, we must be logged in with a user account to authorize the API request.
Let’s run the code below and follow the steps to complete the authorization process, and consequently add a comment.
{{CLIENT_SECRET}}
The API response object will have a nested snippet
object, which contains the videoId
of the video we want to comment on. We can also see information about the comment itself, including the comment’s text, name of the comment author, published date, and so on. We can verify the addition of the comment by looking at the ”Comments” section of the YouTube video.