The video resource represents a YouTube video and can be used to get a video’s statistics. The statistics can include the view count, like count, dislike count, or the comment count of a video.

Supported methods

Some methods supported by the API for the video resource are listed below:

  • list: Returns a list of videos specified in the API call that match the request parameters.
  • insert: Uploads a video to a YouTube channel with an option to set the video’s metadata.
  • delete: Deletes a YouTube video.
  • rate: Adds or removes a like or dislike rating for a video.

Note: The resource also supports methods like getRating and update. For this lesson, we’ll take a look at the methods mentioned above.

Let’s look at each method in detail with the help of a few examples.

The list method

It returns a list of videos specified in the API call that match the request parameters. The videos can be retrieved using the id parameter, specifying the YouTube video ID. We can also retrieve a list of videos liked or disliked by the user authorizing the API request.

Let’s see this method in action with the help of a few examples.

Example 1: Retrieve a video’s information using the video ID

In this example, we’ll retrieve information about a video from the Educative Sessions YouTube channel. We require the video ID of any video to retrieve information associated with it.

Note: You can look at the steps in this lesson to find a video’s ID.

You can also replace the id parameter in the code below. To do so, click the “Edit” button in the following widget, enter the video ID in the VIDEO_ID field and click the “Save” button.

Note: By default, the VIDEO_ID has a video ID of a video from Educative’s YouTube channel.

Let’s run the following code to get a response with information about the video.

Press + to interact
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.videos().list(
part="snippet,statistics",
id="{{VIDEO_ID}}"
)
response = request.execute()

Example 2: Retrieve videos liked by an authenticated user

This example retrieves a list of videos liked by the user authorizing the API request.

Note: We’ll use the myRating parameter with its value set to like, which means that we only want a list of videos liked by the authorized user.

Remember that we created an OAuth 2.0 client ID and saved the JSON object of the client_secret_CLIENTID.json file. We’ll use that in this example. If you did not save the object, go ahead and do so in the CLIENT_SECRET field above the code widget given below.

After you click the “Run” button, you’ll see a prompt in the terminal that says “Please visit this URL to authorize this application.” Copy that URL and paste it into a browser tab.

You’ll be asked to permit the API request by giving some permissions. Finally, copy the generated code and paste it into the terminal next to the prompt: “Enter the authorization code.”

Finally, you’ll see the API response object in the terminal.

{{CLIENT_SECRET}}
Retrieve videos liked by the user authorizing the API request

The insert method

It uploads a video to YouTube with an option to set the video’s metadata. It requires a video file that has to be uploaded. All properties are optional except for the snippet.title, and snippet.categoryId properties which are required.

Note: We’ll use a sample video already added to the course assets for this course. On line 29 in the code below, we can see the path to the video.

Let’s see this method in action with the help of an example. Here, we will insert a video that has its privacy status set to private (on line 25).

{{CLIENT_SECRET}}
Upload a video to YouTube

When the video is uploaded, we’ll get a JSON response with information about the video. To see the uploaded video with visibility set to private, go to your channel and click the “Manage videos” button in the top-right corner. On the next page, we can see the uploaded video.

The delete method

As the name suggests, the delete method deletes a video. We use the id parameter to specify the ID of the video to be deleted. A video can only be deleted from the account of the user authorizing the API request.

Let’s see this method in action with the help of an example.

Note: For this method to work, you must have a YouTube channel with at least one video that can be deleted.

Add your video’s ID to the USER_VIDEO_ID field in the API key box that appears above the code. Run the code and follow the steps to delete the video.

{{CLIENT_SECRET}}
Deleting a video

Once we’ve received the deletion message in the terminal, we should confirm that the video has been deleted. Go to your channel and click the “Manage videos” button in the top-right corner. On the next page, we can verify that the video has been deleted.

The rate method

The rate method adds a like or dislike rating to a video or removes a rating from a video. We will use the id parameter to specify the video ID that is to be rated. The rating parameter will define the type of rating: like or dislike.

Let’s see this method in action with the help of an example. Here, we’ll rate a video from the Educative Sessions channel. We must authorize this API request since we can only rate a video when logged in as a user account. Let’s run the code below and follow the steps to complete the rating process.

Note: The rating will be a like or a dislike based on the value of the RATING key in the widget below.

{{CLIENT_SECRET}}
Rating a video by the user authorizing the API request

The liked videos are stored in a playlist. Go to this playlist to see the video that you liked.

Note: Make sure you are signed in as the account that you used to like the video.

The video resource is a valuable resource and can add different functionalities to your project. Later in the course, we’ll see how it can be integrated into a real-world app.