In this article, we will learn how to use the YouTube API to retrieve information about a specific video on YouTube, given its video ID. The process involves using Videos.List() method of the YouTubeService class to retrieve the information and specifying the video ID as a parameter.
In a previously published article [YouTube API Integration With C# .NET], we learned how to search for and retrieve a list of videos on YouTube using the YouTube API. The process involves installing the YouTube API client library, obtaining an API key, importing the necessary namespaces and libraries, initializing the YouTube service, and using the Search.List() method of the YouTubeService class to specify the search parameters.
Here is a step-by-step guide on how to use the Videos.List() method to retrieve information about a specific video using the YouTube API in C#:
Step 1. Prerequisites
To Install the YouTube API client library, obtaining an API key, importing the necessary namespaces and libraries, initializing the YouTube service, please see my previous article on [YouTube API Integration With C# .NET].
Step 2. Initialize the YouTubeService Class
The second step is to initialize the YouTubeService class. This class is responsible for making requests to the YouTube API and processing the responses. To initialize the class, you need to pass an instance of the BaseClientService. Initializer class to its constructor. This instance should contain your API key and the name of your application.
YouTubeService youtubeService = new YouTubeService(new BaseClientService.Initializer()
{
ApiKey = "YOUR_API_KEY",
ApplicationName = "YOUR_APPLICATION_NAME"
});
Step 3. Define the Video ID
Next, you need to define the video ID of the video you want to retrieve information about. You can find the video ID in the URL of the video. For example, if the URL of the video is https://www.youtube.com/watch?v=abcdefg"
, the video ID is "abcdefg"
.
string videoId = "VIDEO_ID";
Step 4. Prepare the Request
Use the Videos.List() method to retrieve information about a specific video. Pass the video ID to the method, and set the Part parameter to “snippet, contentDetails, statistics, status”.
VideosResource.ListRequest listRequest = youtubeService.Videos.List("snippet,contentDetails,statistics,status");
listRequest.Id = videoId;
If you pass the parameter "snippet, contentDetails, statistics, status"
to the Part
parameter of the Videos.List()
method, the following information about the video will be returned:
- Snippet
This includes information about the video's title, description, channel information, tags, and the video's publication date.
- ContentDetails
This includes information about the video's duration, aspect ratio, definition, and dimensions.
- Statistics
This includes information about the video's view count, like count, dislike count, comment count, and favourite count.
- Status
This includes information about the video's upload status, privacy status, and license information.
Step 5. Execute the Request
Execute the request by calling the Execute() method of the listRequest object.
VideoListResponse response = listRequest.Execute();
Step 6. Access the Video Information
Finally, you can access the information about the video from the response object. You can use the response object to access information such as the video title, description, view count, and more.
foreach(var item in response.Items) {
Console.WriteLine("Title: " + item.Snippet.Title);
Console.WriteLine("Description: " + item.Snippet.Description);
Console.WriteLine("View Count: " + item.Statistics.ViewCount);
}
Below is the complete code for getting a specific YouTube video:
using Google.Apis.YouTube.v3;
using Google.Apis.YouTube.v3.Data;
using Google.Apis.Services;
using System;
namespace YouTubeAPIDemo {
class Program {
static void Main(string[] args) {
// Initialize the YouTubeService class
YouTubeService youtubeService = new YouTubeService(new BaseClientService.Initializer() {
ApiKey = "YOUR_API_KEY",
ApplicationName = "YOUR_APPLICATION_NAME"
});
// Define the video ID
string videoId = "VIDEO_ID";
// Prepare the request
VideosResource.ListRequest listRequest = youtubeService.Videos.List("snippet,contentDetails,statistics,status");
listRequest.Id = videoId;
try {
// Execute the request
VideoListResponse response = listRequest.Execute();
// Access the video information
foreach(var item in response.Items) {
Console.WriteLine("Title: " + item.Snippet.Title);
Console.WriteLine("Description: " + item.Snippet.Description);
Console.WriteLine("View Count: " + item.Statistics.ViewCount);
}
} catch (Exception e) {
// Log the error
Console.WriteLine("An error occurred: " + e.Message);
}
Console.ReadLine();
}
}
}
Note
You should replace the placeholders "YOUR_API_KEY"
and "YOUR_APPLICATION_NAME"
with your actual API key and application name, and replace "VIDEO_ID"
with the actual video ID of the video, you want to retrieve information about.
The code will result in the following.
So here we have seen a very basic example of how to get video details by Video ID.
In conclusion, YouTube API's Videos resource provides an efficient way for developers to access and retrieve information about videos.
You can explore more by clicking here to YouTube API documentation for the Video's resource. The Videos resource provides methods for retrieving information about videos on YouTube, including the video's metadata and status, as well as information about the channel that uploaded the video. The resource provides methods for retrieving specific videos by ID, as well as methods for retrieving a list of videos that match specific criteria. The documentation provides detailed information about the available parameters for each method, as well as information about the format of the returned data.
I hope you will find this article helpful. If you have any suggestions, then please feel free to ask in the comment section.
Thank you.