Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get Videos from a Channel using Youtube API V3 in C#

I have a ASP.Net webpage where I was displaying Youtube Videos from my channel using V2. Since Google has retired V2 API, I am trying to use V3 API, but unable to get the videos from the channel.

I did look at the samples at github, but the example shows how to create a video, but no way to retrieve videos. Searching on SO, I see examples using php library, I am looking something specific to C#.

Can anyone help me with regard to this?

like image 841
Nitesh Avatar asked Dec 25 '22 17:12

Nitesh


2 Answers

By adding channel id to Search.list it returns a list of the videos in the channel.

var searchListRequest = service.Search.List("snippet");
searchListRequest.ChannelId = "UCIiJ33El2EakaXBzvelc2bQ";
var searchListResult = searchListRequest.Execute();

Update response to comment explanation to what is happening:

Actually search returns everything associated with the channel id, you are after all searching on a channel id.

Search returns a SearchListResponse which contains a number of items. Each item is of type SearchResource search resources can have different types or Kinds. In the two pictures below you can see that the first one is kind youtube#channel the second is kind youtube#video it will be up to you to loop though them and find the youtube videos. if you scroll to the bottom of the search.list page you can try it and see the raw JSon that the API is returning.

enter image description here

enter image description here

solution:

Now if all you want to do is get the videos back you can just tell it that all you want are videos by adding type to your request:

searchListRequest.Type = "video";
like image 106
DaImTo Avatar answered Dec 31 '22 13:12

DaImTo


Although asked some time ago I also have been searching for a while how to get videos ( all ) from channel using C#. At the moment I have method that supports pagination (probably can be done better :) )

Hope this helps

    public Task<List<SearchResult>> GetVideosFromChannelAsync(string ytChannelId)
    {

        return Task.Run(() =>
        {
            List<SearchResult> res = new List<SearchResult>();

var _youtubeService = new YouTubeService(new BaseClientService.Initializer()
        {
            ApiKey = "AIzaXyBa0HT1K81LpprSpWvxa70thZ6Bx4KD666",
            ApplicationName = "Videopedia"//this.GetType().ToString()
        });

            string nextpagetoken = " ";

            while (nextpagetoken != null)
            {
                var searchListRequest = _youtubeService.Search.List("snippet");
                searchListRequest.MaxResults = 50;
                searchListRequest.ChannelId = ytChannelId;
                searchListRequest.PageToken = nextpagetoken;

                // Call the search.list method to retrieve results matching the specified query term.
                var searchListResponse = searchListRequest.Execute();

                // Process  the video responses 
                res.AddRange(searchListResponse.Items);

                nextpagetoken = searchListResponse.NextPageToken;

            }

            return res;

        });
    } 
like image 29
erPe Avatar answered Dec 31 '22 14:12

erPe