I am trying like below but in all Items it is showing count = 0. I have deliberately written htps since it does not allow me to Post.
C#
StringBuilder htmlTable = new StringBuilder();
var client = new RestClient("googleapis.com/youtube/v3");
var request = new RestRequest("search", Method.GET);
request.AddParameter("part", "snippet");
request.AddParameter("type", "video");
request.AddParameter("channelId", "UCg3Zp-m_nNVw");
request.AddParameter("key", "AIzaSu9f5blG8");
List allItems = new List();
// Make initial request for youtube endpoint
IRestResponse response = client.Execute(request);
htmlTable.Append("");
htmlTable.Append("Video IdVideo TitleDescriptionViewsLikeCommentPublished");
htmlTable.Append("");
foreach (var data in allItems.OrderBy(x => x.snippet.publishedAt))Code
Thanks
Jithu ThomasRank #437 · 5.7K reputationPosted Aug 22, 2024, 10:47 AM EDT
Can you please update as following and try
StringBuilder htmlTable = new StringBuilder(); var client = new RestClient("https://www.googleapis.com/youtube/v3"); var searchRequest = new RestRequest("search", Method.GET); searchRequest.AddParameter("part", "snippet"); searchRequest.AddParameter("type", "video"); searchRequest.AddParameter("channelId", "UCg3Zp-m_nNVw"); searchRequest.AddParameter("key", "AIzaSu9f5blG8"); // Make initial request for YouTube search endpoint IRestResponse searchResponse = client.Execute(searchRequest); List allItems = new List(); // Check if the search response was successful if (searchResponse.IsSuccessful && searchResponse.Data != null) { // Add items to the list allItems.AddRange(searchResponse.Data.items); } htmlTable.Append(""); htmlTable.Append("Video IdVideo TitleDescriptionViewsLikesCommentsPublished"); htmlTable.Append(""); foreach (var data in allItems.OrderBy(x => x.snippet.publishedAt)) { // Get the video ID string videoId = data.id.videoId; // Prepare the request to get statistics for the video var videoRequest = new RestRequest("videos", Method.GET); videoRequest.AddParameter("part", "statistics"); videoRequest.AddParameter("id", videoId); videoRequest.AddParameter("key", "AIzaSu9f5blG8"); // Execute the request IRestResponse videoResponse = client.Execute(videoRequest); int viewCount = 0; int likeCount = 0; int commentCount = 0; // Check if the video response was successful if (videoResponse.IsSuccessful && videoResponse.Data != null && videoResponse.Data.items.Count > 0) { var statistics = videoResponse.Data.items[0].statistics; viewCount = statistics.viewCount; likeCount = statistics.likeCount; commentCount = statistics.commentCount; } // Append the data to the HTML table htmlTable.Append(""); htmlTable.Append($"{videoId}"); htmlTable.Append($"{data.snippet.title}"); htmlTable.Append($"{data.snippet.description}"); htmlTable.Append($"{viewCount}"); htmlTable.Append($"{likeCount}"); htmlTable.Append($"{commentCount}"); htmlTable.Append($"{data.snippet.publishedAt.ToString("yyyy-MM-dd")}"); htmlTable.Append(""); } htmlTable.Append(""); htmlTable.Append(""); // Output the HTML table (you would typically return this in a view or write it to the response) string resultHtml = htmlTable.ToString();