Hi
I have below code . I want Category wise most viewed youtube videos
C#
using (var yt = new YouTubeService(new BaseClientService.Initializer()
{
ApiKey = "AIzaSyKu9f5blF7idak6cGul59UQ7c7Ls8ECKT8",
}))
{
StringBuilder htmlTable = new StringBuilder();
var request = yt.Search.List("snippet");
request.Order = Google.Apis.YouTube.v3.SearchResource.ListRequest.OrderEnum.ViewCount;
request.PublishedAfter = DateTime.Today;
request.PublishedBefore = DateTime.Today.AddDays(1);
var response = request.Execute();
var objResult = response.Items.ToList();Thanks

Naimish MakwanaRank #324 · 7.7K reputationPosted Jan 31, 2023, 10:34 AM EST
Hello Ramco,
use below code. Its working fine and tested.
request.Type = "video"; request.VideoCategoryId = "10";need to use small "v" instead of Capital "V".
Working code.
private void Run() { var youtubeService = new YouTubeService(new BaseClientService.Initializer() { ApiKey = "REPLACE_ME", ApplicationName = this.GetType().ToString() }); var searchListRequest = youtubeService.Search.List("snippet"); searchListRequest.PublishedAfter = DateTime.Today; searchListRequest.PublishedBefore = DateTime.Today.AddDays(1); searchListRequest.Order = Google.Apis.YouTube.v3.SearchResource.ListRequest.OrderEnum.ViewCount; searchListRequest.Type = "video"; searchListRequest.VideoCategoryId = "10"; // Call the search.list method to retrieve results matching the specified query term. var searchListResponse = searchListRequest.Execute(); List videos = new List(); List channels = new List(); List playlists = new List(); // Add each result to the appropriate list, and then display the lists of // matching videos, channels, and playlists. foreach (var searchResult in searchListResponse.Items) { switch (searchResult.Id.Kind) { case "youtube#video": videos.Add(String.Format("{0} ({1})", searchResult.Snippet.Title, searchResult.Id.VideoId)); break; case "youtube#channel": channels.Add(String.Format("{0} ({1})", searchResult.Snippet.Title, searchResult.Id.ChannelId)); break; case "youtube#playlist": playlists.Add(String.Format("{0} ({1})", searchResult.Snippet.Title, searchResult.Id.PlaylistId)); break; } } Console.WriteLine(String.Format("Videos:\n{0}\n", string.Join("\n", videos))); Console.WriteLine(String.Format("Channels:\n{0}\n", string.Join("\n", channels))); Console.WriteLine(String.Format("Playlists:\n{0}\n", string.Join("\n", playlists))); }Please mark answer accepted if its useful.
Thanks
Naimish
Sachin SinghRank #66 · 26.2K reputationPosted Jan 31, 2023, 6:46 AM EST
you can use VideoCategoryId
Ramco RamcoRank #462 · 5.4K reputationPosted Jan 31, 2023, 7:31 AM EST
Hi Sachin
It is giving below error on below line;
request.VideoCategoryId = "10";
It is giving below error - Google.GoogleApiExceptiond__35.MoveNext()
HResult=0x80131500
Message=The service youtube has thrown an exception. HttpStatusCode is BadRequest. Request contains an invalid argument.
Source=Google.Apis
StackTrace:
at Google.Apis.Requests.ClientServiceRequest`1.
at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
at Google.Apis.Requests.ClientServiceRequest`1.Execute()
at MyChannel.Admin.MyVideos.MostViewdVideos() in E:\MyChannel\MyChannel\Admin\MyVideos.aspx.cs:line 52
at MyChannel.Admin.MyVideos.Page_Load(Object sender, EventArgs e) in
Ramco RamcoRank #462 · 5.4K reputationPosted Jan 31, 2023, 9:32 AM EST
Hi
It is giving same error on - request.VideoCategoryId = "10";
Thanks
Ramco RamcoRank #462 · 5.4K reputationPosted Jan 31, 2023, 9:55 AM EST
Hi Sachin
It is giving error Cannot implicitly convert type int to string on second line
request.Type = "Video";
request.VideoCategoryId = 10;
Thanks
Ramco RamcoRank #462 · 5.4K reputationPosted Jan 31, 2023, 10:17 AM EST
Hi Naimish
Then it gives below error
It is giving below error - Google.GoogleApiExceptiond__35.MoveNext()
HResult=0x80131500
Message=The service youtube has thrown an exception. HttpStatusCode is BadRequest. Request contains an invalid argument.
Source=Google.Apis
StackTrace:
at Google.Apis.Requests.ClientServiceRequest`1.
at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
at Google.Apis.Requests.ClientServiceRequest`1.Execute()
at MyChannel.Admin.MyVideos.MostViewdVideos() in E:\MyChannel\MyChannel\Admin\MyVideos.aspx.cs:line 52
at MyChannel.Admin.MyVideos.Page_Load(Object sender, EventArgs e) in
Thanks
Naimish MakwanaRank #324 · 7.7K reputationPosted Jan 31, 2023, 9:16 AM EST
Try below code.
request.VideoCategoryId = "" request.VideoType = "any"; //Acceptable values are: any, episode, movieThanks
Naimish MakwanaRank #324 · 7.7K reputationPosted Jan 31, 2023, 10:02 AM EST
Error is due to you are assing int to string type.
try this.
request.VideoCategoryId = "10";