Rate and Comment on a Video
Learn how to comment and rate a video using Youtube Data API.
We'll cover the following...
Commenting on and rating a video is a good way to increase user engagement. It’s also a good way for the content creator to estimate whether their content is making any impact on their audience or not.
In this lesson, we’ll learn to interact with a video by rating or commenting on it with the help of YouTube Data API.
Rate a video
A user can use the rate method to post their rating about the video by sending a POST request to the following URL:
https://www.googleapis.com/youtube/v3/videos/rate
Here are the two parameters that we can use to call the endpoint:
Request parameters
Name | Type | Category | Description |
| String | Required | Specifies the video user wants to rate. |
| String | Required | Specifies the rating user wants to give to the video. The possible values are |
Let’s give a thumbs-up to our video on the channel. Click the “Run” button to execute the following code:
Lines 13–16: We define the
bodyParametersvariable in which we define two parameters:idandrating. Theidparameter is used to define the ID of the video that the user wants to rate and theratingparameter allows the user to pass their rating.Line 24: We define an
asyncfunction with the name ofrateVideo()to call the endpoint.
In the case of successful execution of the above code, it will return the response code 204.
Comment on a video
In order to post a comment, we use the insert method of the CommentThreads resource, which will be used to create the top-level comment on a video.
We'll send the POST request to the following URL to post a comment on a video:
https://www.googleapis.com/youtube/v3/commentThreads
Request parameters
Name | Type | Category | Description |
| String | Required | This parameter serves two functions. It specifies the |
| String | Required | Used to provide the channel ID. |
| String | Required | Used to provide the comment user wants to post. |
| String | Optional | Used to provide the video ID. |
We need to add our comment text in the snippet.topLevelComment.snippet.textOriginal parameter in the below widget.
Click the “Run” button on the below widget to add a comment to the video.
Let’s understand the above program by breaking it down:
Lines 8–11: We initialize the
headerParametersvariable in which we pass the required headers.Lines 17–27: We define the
snippet.channelIdandsnippet.videoIdinbodyParameters. Additionally, we pass our comment insnippet.topLevelComment.snippet.textOriginal.Line 37: We define an
asyncfunction namedpostComment()to post a comment on the video.
The following table contains some of the important response fields:
Response fields
Name | Type | Description |
| String | Reflects the resource type of the API call. |
| ETag | Contains the resource ETag. |
| String | Contains the comment ID. |
| Object | Contains the comment thread details. |
List the comments
We can also list all the comments on the particular video or channel by sending a GET request to the following URL:
https://www.googleapis.com/youtube/v3/commentThreads
Here are some parameters that we can use to call the endpoint:
Request parameters
Name | Type | Category | Description |
| String | Required | This parameter specifies a comma-separated list of one or more resource properties to be included in the API response. These are |
| String | Optional | Used to filter the comment threads that are about a specific channel. |
| String | Optional | Specifies comma-separated IDs of the comment threads to list thier properties in the API response. |
| String | Optional | Filters the comment threads of a specific video. |
| String | Optional | Filters all the comment threads of a specific channel. |
| Integer | Optional | Fixes the number of results that will be fetched. |
| String | Optional | Specifies the page in the response. |
Note: In order to call this endpoint, you have to define one of the parameters from
videoId,channelId,id, orallThreadsRelatedToChannelId.
Click the “Run” button to list the comments on the videos.
Let’s understand the above code by breaking it down:
Lines 14–17: We define the
queryParametersin which we define thepartandvideoIdparameters.Line 26: We define an
asyncfunction aslistComment()to retrieve comments from the video.
The following table contains some of the important response fields:
Response fields
Name | Type | Description |
| String | Reflects the resource type of the API call. |
| ETag | Contains the resource ETag. |
| String | Used to fetch the next page in the response result. |
| String | Contains the comment text. |
| String | Used to fetch the previous page in the response result. |
| Object | Contains the response page information. |
| List | Contains the list of the result that matches the request requirement. |
Delete a comment
In this part of the lesson, we’ll use the delete method of the Comments resource to delete the comment by sending the DELETE request to the following URL:
https://www.googleapis.com/youtube/v3/comments
In order to delete a comment on a video, we only need the ID of the comment that we want to delete. Let’s delete the comment we posted on our video earlier. Click the “Run” button to delete a comment.
Let’s understand the above code by breaking it down:
Line 13: We define the
idparameter in which we pass the comment ID that we want to delete.Lines 16–19: We define an
optionsvariable in which we defined the request type asDELETE, and the header in which we passed theheaderParametersvariable.
In case of successful execution of the above code, it will return the response code 204.