DEV Community

Cover image for How to search for YouTube videos in a channel with .NET Core 3
Davide Bellone
Davide Bellone

Posted on • Originally published at code4it.dev

How to search for YouTube videos in a channel with .NET Core 3

Let's say that you have a YouTube channel. Now, you want to list all the videos that you uploaded, and you are really strong in .NET Core 3.

So, what can you do?

First of all, you need to retrieve two information: your API key and your channel id.

Create your YouTube API Key

In order to use YouTube APIs, you need a developer key.

You can get it by simply visiting the Google developers page, where you can set up a new project (it's just a grouping of functionalities) and then navigate to the Credentials page where you can create the key.

Credentials page for getting API keys

Here you can set additional properties, like the key name and some restrictions.

Get you Channel ID

This information is really simple to get. Just access to your account, then navigate to your advanced settings.

YouTube channel settings

Here you have info about your user id and your channel id.

Ok, now we have everything we need! It's time to go!

First steps with your codebase

Of course, you need an application to host the code. You can simply create a Console application with .NET Core 3.

Then, you need to download and install two NuGet packages: Google.Apis and Google.Apis.YouTube.v3; currently both packages are at version v1.43.0.

Now, since you need a list of videos, you must create a YouTubeVideo class:

public class YouTubeVideo
{
    public string Id { get; set; }
    public string Title { get; set; }
    public string Description { get; set; } 
    public string Thumbnail { get; set; }
    public string Picture { get; set; }
}
Enter fullscreen mode Exit fullscreen mode

Nothing difficult. Just notice that I have 2 fields for the images: one for the Thumbnail and another for the main Picture.

Add a YouTube Service

To download those data, you need to instantiate a YouTube Service:

using (var youtubeService = new YouTubeService(new BaseClientService.Initializer()
{
    ApiKey = '<your api key>'
}))
{
    // todo 
}
Enter fullscreen mode Exit fullscreen mode

NOTE: here I only set the ApiKey value in the Initializer constructor. You can add other properties, like GZipEnabled, ApplicationName, Serializer and so on.

Download the list of result

Now that you have the client, you must create the request.

var searchListRequest = youtubeService.Search.List("snippet");
searchListRequest.Q = searchText;
searchListRequest.MaxResults = maxResult;
searchListRequest.ChannelId = '<your-channel-id>';
searchListRequest.Type = "video";
searchListRequest.Order = SearchResource.ListRequest.OrderEnum.Relevance;
Enter fullscreen mode Exit fullscreen mode

The YouTubeService class contains references to most of the capabilities provided by YouTube, such as Search, Video details, Channels, Captions and so on. Here we are interested in the Search functionality, so we must set it as "endpoint" and define where we want to search.

Confession time! I still haven't figured out what the snippet value means, and if there are other values. Every example in the documentation use this value.

The parameters meaning is straightforward. But just a consideration: since the service searches for each part of YouTube, you can restrict the type of content that you want. Here I set "video", but you can use a combination of "video", "channel" and "playlist", separated by a comma.

Now that we have set our search parameters, we can get the results:

var searchListResponse = await searchListRequest.ExecuteAsync();
Enter fullscreen mode Exit fullscreen mode

This method returns a SearchListResponse object that contains items in the Items field.

Now you have everything you need, so you can generate the list of YouTubeVideo elements this way:

List<YouTubeVideo> videos = new List<YouTubeVideo>();
string GetThumbnailImg(ThumbnailDetails thumbnailDetails)
{
    if (thumbnailDetails == null)
        return string.Empty;
    return (thumbnailDetails.Medium ?? thumbnailDetails.Default__ ?? thumbnailDetails.High)?.Url;
}


string GetMainImg(ThumbnailDetails thumbnailDetails)
{
    if (thumbnailDetails == null)
        return string.Empty;
    return (thumbnailDetails.Maxres ?? thumbnailDetails.Standard ?? thumbnailDetails.High)?.Url;
}

foreach (var responseVideo in searchListResponse.Items)
{
    videos.Add(new YouTubeVideo()
    {
        Id = responseVideo.Id.VideoId, 
        Description = responseVideo.Snippet.Description,
        Title = responseVideo.Snippet.Title,
        Picture = GetMainImg(responseVideo.Snippet.Thumbnails),
        Thumbnail = GetThumbnailImg(responseVideo.Snippet.Thumbnails)
    });
}
Enter fullscreen mode Exit fullscreen mode

Notice: most of the interesting properties of a video are stored in the inner fields, like Id.VideoId or Snippet.Description. Also, notice that each element has 4 thumbnails, one for each resolution. Since some of them might be empty, you should select a fallback value.

Final result

Now you have all the info you need! You can have a look at a complete example on my GitHub account. I have done a bit of refactoring: now the method is wrapped into an async method, and the configurations are stored outside the method.

The pagination issue

As you might have noticed, I didn't mention the pagination.

YouTube uses a curious way to handle it: instead of allowing you to set the current page (or the typical skip and limit parameters), it returns a token for the next page and a token for the previous page.

In fact, the searchListResponse contains two properties, NextPageToken and PrevPageToken that must be used for the pagination. So you must save the token somewhere and use it as a parameter of searchListRequest.


// when you read the content
var nextPageToken = searchListResponse.NextPageToken;

// when you are querying next page
searchListRequest.PageToken = nextPageToken;

Enter fullscreen mode Exit fullscreen mode

Wrapping up

Here I searched for content on YouTube using .NET Core 3.
Just remember that the result data are about the search preview, so the description and the images are not the same that you would retrieve by analyzing the detail of a single video. For example, the description field here is a shortened version of the real description of the video. You will see how to get those details in a separate article.


Previously posted on code4it.dev

Top comments (0)