Introduction
This is one of the social media implementations. Here we are fetching all the videos from a particular YouTube channel and showing in a console application
Description
We need to follow the following steps for implementing this application.
Step-1(Create an API Key)
Now before moving forward we should have a Google Developer account (e.g. any Google account).
Visit this
URL. And it will come up like the following image.
Now click on Create Project giving a project name and click Create. After this do as per the following image and follow the instructions.
After completing this a popup window will come and then click the Browser key. Now give a name and click Create. After this, you will get your API key by which you are going to get all the videos from a particular YouTube channel.
Step-2(Get Channel Name)
While fetching all the details of a particular channel we need to know the Channel Name.
See I am a die heart fan of Mumbai-Indians. Go to YouTube.com search Mumbai Indians as in the following image:
You can see the 1st one is the channel as it has been written. Click on it you will get a URL as
https://www.youtube.com/user/mipaltan.
Here
mipaltan is the channel name. You can take any of your preferred channel names in this process.
Create a console application using visual studio.
Search and Install YouTube.V3 from the Nuget Package Manager Console.
Add your code as per below
- class Program
- {
- static void Main(string[] args)
- {
- try
- {
- var yt = new YouTubeService(new BaseClientService.Initializer() { ApiKey = "AIzaSyCT8kXaxJ2l29vYg4HBdYy36H-PhAH-Teg" });
- var channelsListRequest = yt.Channels.List("contentDetails");
- channelsListRequest.ForUsername = "mipaltan";
- var channelsListResponse = channelsListRequest.Execute();
- int VideoCount=1;
- foreach (var channel in channelsListResponse.Items)
- {
- var uploadsListId = channel.ContentDetails.RelatedPlaylists.Uploads;
- var nextPageToken = "";
- while (nextPageToken != null)
- {
- var playlistItemsListRequest = yt.PlaylistItems.List("snippet");
- playlistItemsListRequest.PlaylistId = uploadsListId;
- playlistItemsListRequest.MaxResults = 50;
- playlistItemsListRequest.PageToken = nextPageToken;
-
- var playlistItemsListResponse = playlistItemsListRequest.Execute();
- foreach (var playlistItem in playlistItemsListResponse.Items)
- {
- Console.WriteLine("Sl No={0}", VideoCount);
- Console.Write("Video ID ={0} ", "https://www.youtube.com/embed/" + playlistItem.Snippet.ResourceId.VideoId);
-
-
-
-
- VideoCount++;
- }
- nextPageToken = playlistItemsListResponse.NextPageToken;
- }
- Console.ReadLine();
- }
- }
- catch (Exception e)
- {
- Console.WriteLine("Some exception occured" + e);
- }
- }
- }
Note
- This code will list the Serial Number and Video ID of all 202 videos.
- You can also add the commented things to get all the details.
For getting the latest video from a YouTube channel using Jquery find the
link
You can download the attached code sample for reference. Hope that helps you.
Download the solution from this
link.