Hi
Ihave below code for own Videos. I want to display Views , Like Comments & category name also.
C#
using (var yt = new YouTubeService(new BaseClientService.Initializer()
{
ApiKey = "ApiKey",
}))
{
StringBuilder htmlTable = new StringBuilder();
var request = yt.Search.List("snippet");
request.ChannelId = "ChannelId";
request.Type = "video";
var response = request.Execute();
var objResult = response.Items.ToList();
}Thanks

Naimish MakwanaRank #324 · 7.7K reputationPosted Feb 21, 2023, 9:28 AM EST
Hello Ramco,
You need to add your channel id in parameter
var client = new RestClient("https://www.googleapis.com/youtube/v3"); var request = new RestRequest("search", Method.Get); request.AddParameter("part", "snippet"); request.AddParameter("q", "sports shorts"); //add this parameter request.AddParameter("channelId", "YOUR_CHANNEL_ID"); . . . //Rest of codeThanks
Naimish Makwana
Ramco RamcoRank #462 · 5.4K reputationPosted Feb 21, 2023, 4:30 AM EST
Hi Naimish
It is giving error - Object reference not set to an instance of the object. I want list of all my Videos.
var clientTag = new RestClient("https://www.googleapis.com/youtube/v3/"); var tagRequest = new RestRequest("videos", Method.GET); tagRequest.AddParameter("key", "ApiKey8"); tagRequest.AddParameter("part", "snippet,statistics"); tagRequest.AddParameter("maxResults", 100); tagRequest.AddParameter("order", "viewCount"); //tagRequest.AddParameter("id", ""); var tagResponse = clientTag.Execute(tagRequest);Thanks
Ramco RamcoRank #462 · 5.4K reputationPosted Feb 21, 2023, 4:56 AM EST
Hi Naimish
Below is the code
foreach (var item in tagResponse.Data.items)
protected void MyOwnVideos() { var clientTag = new RestClient("googleapis.com/youtube/v3/"); var tagRequest = new RestRequest("videos", Method.GET); tagRequest.AddParameter("key", "ApiKey"); tagRequest.AddParameter("part", "snippet,statistics"); tagRequest.AddParameter("maxResults", 100); tagRequest.AddParameter("order", "viewCount"); //tagRequest.AddParameter("id", ""); var tagResponse = clientTag.Execute(tagRequest); foreach (var item in tagResponse.Data.items) { Console.WriteLine("viewCount" + item.statistics.viewCount); Console.WriteLine("likeCount" + item.statistics.likeCount); Console.WriteLine("commentCount" + item.statistics.commentCount); var categoryId = item.snippet.categoryId; var clientCategory = new RestClient("googleapis.com/youtube/v3/"); var categoryRequest = new RestRequest("videoCategories", Method.GET); categoryRequest.AddParameter("key", "A"); categoryRequest.AddParameter("part", "snippet"); categoryRequest.AddParameter("id", categoryId); var categoryResponse = clientCategory.Execute(categoryRequest); if (categoryResponse.Data?.Items != null) { Console.WriteLine("CategoryName " + categoryResponse.Data.Items[0].Snippet.Title); } } }Thanks
Ramco RamcoRank #462 · 5.4K reputationPosted Feb 21, 2023, 5:10 AM EST
Hi Naimish
In Api Url i have deliberately written. It does not allow me to post.
Secondly i want to display all my Videos list
Thanks
Naimish MakwanaRank #324 · 7.7K reputationPosted Feb 21, 2023, 3:21 AM EST
Hello Ramco,
Use below code
var clientTag = new RestClient("https://www.googleapis.com/youtube/v3/"); var tagRequest = new RestRequest("videos", Method.Get); tagRequest.AddParameter("key", "API_KEY"); tagRequest.AddParameter("part", "snippet,statistics"); tagRequest.AddParameter("maxResults", 100); request.AddParameter("order", "viewCount"); tagRequest.AddParameter("id", ""); var tagResponse = clientTag.Execute(tagRequest); foreach (var item in tagResponse.Data.items) { Console.WriteLine("viewCount" + item.statistics.viewCount); Console.WriteLine("likeCount" + item.statistics.likeCount); Console.WriteLine("commentCount" + item.statistics.commentCount); var categoryId = item.snippet.categoryId; var clientCategory = new RestClient("https://www.googleapis.com/youtube/v3/"); var categoryRequest = new RestRequest("videoCategories", Method.Get); categoryRequest.AddParameter("key", "ApiKey8"); categoryRequest.AddParameter("part", "snippet"); categoryRequest.AddParameter("id", categoryId); var categoryResponse = clientCategory.Execute(categoryRequest); if (categoryResponse.Data?.Items != null) { Console.WriteLine("CtegoryName " + categoryResponse.Data.Items[0].Snippet.Title); } }Thanks
Naimish
Naimish MakwanaRank #324 · 7.7K reputationPosted Feb 21, 2023, 4:36 AM EST
Hello Ramco,
Please provide full code. and highlight the line where you get the error. It will help to get more understanding on error.
Thanks
Naimish
Naimish MakwanaRank #324 · 7.7K reputationPosted Feb 21, 2023, 5:04 AM EST
Hello Ramco,
There are two issues with your code.
new RestClient("https://www.googleapis.com/youtube/v3")2. You need to pass Video id in request as you are using Video api.
Thanks
Naimish Makwana
Naimish MakwanaRank #324 · 7.7K reputationPosted Feb 21, 2023, 5:12 AM EST
If you wan for all videos. Then use below code.
Change the parametes as per your requirement. Ex you can remove publishedAfter param if you don't want it.
var client = new RestClient("https://www.googleapis.com/youtube/v3"); var request = new RestRequest("search", Method.Get); request.AddParameter("part", "snippet"); request.AddParameter("q", "sports shorts"); request.AddParameter("type", "video"); request.AddParameter("maxResults", 50); request.AddParameter("order", "viewCount"); request.AddParameter("videoDuration", "short"); var today = DateTime.Now.Date; var sevenDaysAgo = today.AddDays(-1).ToString("yyyy-MM-ddTHH:mm:ssZ"); request.AddParameter("publishedAfter", sevenDaysAgo); request.AddParameter("key", "API_KEY"); var response = client.Execute(request); List listVideo = new List(); foreach (var data in response.Data?.items) { var clientTag = new RestClient("https://www.googleapis.com/youtube/v3/"); var tagRequest = new RestRequest("videos", Method.Get); tagRequest.AddParameter("key", "API_KEY"); tagRequest.AddParameter("part", "snippet,statistics"); tagRequest.AddParameter("maxResults", 100); request.AddParameter("order", "viewCount"); tagRequest.AddParameter("id", data.id.videoId); var tagResponse = clientTag.Execute(tagRequest); foreach (var item in tagResponse.Data.items) { Console.WriteLine("viewCount" + item.statistics.viewCount); Console.WriteLine("likeCount" + item.statistics.likeCount); Console.WriteLine("commentCount" + item.statistics.commentCount); var categoryId = item.snippet.categoryId; var clientCategory = new RestClient("https://www.googleapis.com/youtube/v3/"); var categoryRequest = new RestRequest("videoCategories", Method.Get); categoryRequest.AddParameter("key", "ApiKey8"); categoryRequest.AddParameter("part", "snippet"); categoryRequest.AddParameter("id", categoryId); var categoryResponse = clientCategory.Execute(categoryRequest); if (categoryResponse.Data?.Items != null) { Console.WriteLine("CtegoryName " + categoryResponse.Data.Items[0].Snippet.Title); } } }Thanks
Naimish
Ramco RamcoRank #462 · 5.4K reputationPosted Feb 21, 2023, 8:09 AM EST
Hi Naimish
I want to view my own channel videos. What code i need to add
Thanks