Hi
I have below code . I want to list No of Subscribers also.
C#
protected void MyOwnVideos()
{
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", "UC_nNoaVw"); // Replace with your actual channelId
request.AddParameter("key", "AIz8..."); // Replace with your actual API key
IRestResponse response = client.Execute(request);
// Check if the response contains data
if (response.Data == null || response.Data.items == null || response.Data.items.Count == 0)
{
Console.WriteLine("No Data Found. Please check your channelId and API key.");
return;
}
// Build the HTML table
htmlTable.Append("");
htmlTable.Append("Video IdVideo TitleDescriptionPublished");
htmlTable.Append("");
foreach (var data in response.Data.items.OrderBy(x => x.snippet.publishedAt))
{
htmlTable.Append("");
htmlTable.Append($"{data.id}");
htmlTable.Append($"{data.snippet.title}");
htmlTable.Append($"{data.snippet.description}");
htmlTable.Append($"{data.snippet.publishedAt}");
htmlTable.Append("");
}
htmlTable.Append("");
htmlTable.Append("");
// Output the HTML table
Console.WriteLine(htmlTable.ToString());
}
public class YoutubeSearchListResponse
{
public string kind { get; set; }
public string etag { get; set; }
public string nextPageToken { get; set; }
public string regionCode { get; set; }
public PageInfo pageInfo { get; set; }
public List items { get; set; }
}
public class Snippet
{
public DateTime publishedAt { get; set; }
public string channelId { get; set; }
public string title { get; set; }
public string description { get; set; }
public Thumbnail thumbnails { get; set; }
public string channelTitle { get; set; }
public string liveBroadcastContent { get; set; }
public DateTime publishTime { get; set; }
}
public class Id
{
public string kind { get; set; }
public string videoId { get; set; }
}
public class Item
{
public string kind { get; set; }
public string etag { get; set; }
public Id id { get; set; }
public Snippet snippet { get; set; }
}Thanks

Emily FosterRank #3,495 · 415 reputationPosted Feb 16, 2025, 8:46 AM EST
It appears you are looking to extract the number of subscribers for a specific YouTube channel. In the given code snippet, you are retrieving video details based on a provided channel ID. To get the number of subscribers, you would typically need to make use of the YouTube Data API endpoint specifically designed to fetch channel information, including subscriber count.
To achieve this, you can modify your existing code to include an additional request to fetch the channel information. Here is an outline of steps you might take:
1. Update the endpoint to retrieve channel information based on the channel ID.
2. Parse the response to extract the subscriber count from the channel data.
3. Incorporate the subscriber count into your HTML table or output as needed.
Here's a simplified example of how you could modify your existing code snippet to include subscriber count retrieval:
// Make a separate request to get channel information var channelInfoRequest = new RestRequest("channels", Method.GET); channelInfoRequest.AddParameter("part", "snippet,statistics"); channelInfoRequest.AddParameter("id", "UC_nNoaVw"); // Replace with your actual channelId channelInfoRequest.AddParameter("key", "AIz8..."); // Replace with your actual API key IRestResponse channelResponse = client.Execute(channelInfoRequest); // Check if the channel response contains data if (channelResponse.Data == null || channelResponse.Data.items == null || channelResponse.Data.items.Count == 0) { Console.WriteLine("No Channel Data Found. Please check the channelId and API key."); return; } // Extract subscriber count int subscriberCount = channelResponse.Data.items[0].statistics.subscriberCount; // Now you can use the 'subscriberCount' in your output along with other video detailsIn this code snippet, `YoutubeChannelListResponse` would be a class similar to `YoutubeSearchListResponse` but tailored to channel information. By fetching the channel statistics, including the subscriber count, you can enrich your video listing with this additional data.
Feel free to integrate this logic into your existing code and adapt it to best suit your requirements. Let me know if you need further clarification or assistance with the implementation!
Muhammad Imran AnsariRank #326 · 7.6K reputationPosted Feb 16, 2025, 11:33 AM EST
Hello Ramco,
To include the number of subscribers for the YouTube channel in your code, you need to make an additional API call to the
channelsendpoint of the YouTube Data API. This endpoint provides channel statistics, including the subscriber count.Here’s how you can modify your code to include the number of subscribers:
Add a method to fetch channel statistics.
Update the
MyOwnVideosmethod to include the subscriber count.Here’s the updated code:
protected void MyOwnVideos() { StringBuilder htmlTable = new StringBuilder(); var client = new RestClient("https://www.googleapis.com/youtube/v3"); var request = new RestRequest("search", Method.GET); request.AddParameter("part", "snippet"); request.AddParameter("type", "video"); request.AddParameter("channelId", "UC_nNoaVw"); // Replace with your actual channelId request.AddParameter("key", "AIz8..."); // Replace with your actual API key IRestResponse response = client.Execute(request); // Check if the response contains data if (response.Data == null || response.Data.items == null || response.Data.items.Count == 0) { Console.WriteLine("No Data Found. Please check your channelId and API key."); return; } // Fetch channel statistics to get the subscriber count var channelRequest = new RestRequest("channels", Method.GET); channelRequest.AddParameter("part", "statistics"); channelRequest.AddParameter("id", "UC_nNoaVw"); // Replace with your actual channelId channelRequest.AddParameter("key", "AIz8..."); // Replace with your actual API key IRestResponse channelResponse = client.Execute(channelRequest); string subscriberCount = "N/A"; if (channelResponse.Data != null && channelResponse.Data.items != null && channelResponse.Data.items.Count > 0) { subscriberCount = channelResponse.Data.items[0].statistics.subscriberCount; } // Build the HTML table htmlTable.Append(""); htmlTable.Append("Video IdVideo TitleDescriptionPublishedSubscribers"); htmlTable.Append(""); foreach (var data in response.Data.items.OrderBy(x => x.snippet.publishedAt)) { htmlTable.Append(""); htmlTable.Append($"{data.id.videoId}"); htmlTable.Append($"{data.snippet.title}"); htmlTable.Append($"{data.snippet.description}"); htmlTable.Append($"{data.snippet.publishedAt}"); htmlTable.Append($"{subscriberCount}"); htmlTable.Append(""); } htmlTable.Append(""); htmlTable.Append(""); // Output the HTML table Console.WriteLine(htmlTable.ToString()); } public class YoutubeSearchListResponse { public string kind { get; set; } public string etag { get; set; } public string nextPageToken { get; set; } public string regionCode { get; set; } public PageInfo pageInfo { get; set; } public List items { get; set; } } public class YoutubeChannelResponse { public string kind { get; set; } public string etag { get; set; } public List items { get; set; } } public class ChannelItem { public string kind { get; set; } public string etag { get; set; } public string id { get; set; } public ChannelStatistics statistics { get; set; } } public class ChannelStatistics { public string viewCount { get; set; } public string subscriberCount { get; set; } public string hiddenSubscriberCount { get; set; } public string videoCount { get; set; } } public class Snippet { public DateTime publishedAt { get; set; } public string channelId { get; set; } public string title { get; set; } public string description { get; set; } public Thumbnail thumbnails { get; set; } public string channelTitle { get; set; } public string liveBroadcastContent { get; set; } public DateTime publishTime { get; set; } } public class Id { public string kind { get; set; } public string videoId { get; set; } } public class Item { public string kind { get; set; } public string etag { get; set; } public Id id { get; set; } public Snippet snippet { get; set; } } public class PageInfo { public int totalResults { get; set; } public int resultsPerPage { get; set; } } public class Thumbnail { public ThumbnailDetails @default { get; set; } public ThumbnailDetails medium { get; set; } public ThumbnailDetails high { get; set; } } public class ThumbnailDetails { public string url { get; set; } public int width { get; set; } public int height { get; set; } }Good Luck!
niraj3 reputationPosted Feb 16, 2025, 9:12 AM EST
It appears you are looking to extract the number of subscribers for a specific YouTube channel. In the given code snippet, you are retrieving video details based on a provided channel ID. To get the number of subscribers, you would typically need to make use of the YouTube Data API endpoint specifically designed to fetch channel information, including subscriber count.
To achieve this, you can modify your existing code to include an additional request to fetch the channel information. Here is an outline of steps you might take:
1. Update the endpoint to retrieve channel information based on the channel ID.
2. Parse the response to extract the subscriber count from the channel data.
3. Incorporate the subscriber count into your HTML table or output as needed.
Naimish MakwanaRank #324 · 7.7K reputationPosted Feb 16, 2025, 11:53 PM EST
To list the number of subscribers for your YouTube channel, you'll need to make an additional API call to the YouTube Data API to fetch the channel details. Here's how you can modify your code to include the number of subscribers:
MyOwnVideosmethod to call this new method and include the subscriber count in the HTML table.Here's the updated code:
protected void MyOwnVideos() { StringBuilder htmlTable = new StringBuilder(); var client = new RestClient("https://www.googleapis.com/youtube/v3"); var request = new RestRequest("search", Method.GET); request.AddParameter("part", "snippet"); request.AddParameter("type", "video"); request.AddParameter("channelId", "UC_nNoaVw"); // Replace with your actual channelId request.AddParameter("key", "YOUR_API_KEY"); // Replace with your actual API key IRestResponse response = client.Execute(request); // Check if the response contains data if (response.Data == null || response.Data.items == null || response.Data.items.Count == 0) { Console.WriteLine("No Data Found. Please check your channelId and API key."); return; } // Fetch channel details to get the subscriber count var channelDetails = GetChannelDetails("UC_nNoaVw", "YOUR_API_KEY"); if (channelDetails == null) { Console.WriteLine("Failed to fetch channel details."); return; } // Build the HTML table htmlTable.Append(""); htmlTable.Append("Video IdVideo TitleDescriptionPublishedSubscribers"); htmlTable.Append(""); foreach (var data in response.Data.items.OrderBy(x => x.snippet.publishedAt)) { htmlTable.Append(""); htmlTable.Append($"{System.Net.WebUtility.HtmlEncode(data.id.videoId)}"); htmlTable.Append($"{System.Net.WebUtility.HtmlEncode(data.snippet.title)}"); htmlTable.Append($"{System.Net.WebUtility.HtmlEncode(data.snippet.description)}"); htmlTable.Append($"{data.snippet.publishedAt}"); htmlTable.Append($"{channelDetails.statistics.subscriberCount}"); htmlTable.Append(""); } htmlTable.Append(""); htmlTable.Append(""); // Output the HTML table Console.WriteLine(htmlTable.ToString()); } private ChannelDetails GetChannelDetails(string channelId, string apiKey) { var client = new RestClient("https://www.googleapis.com/youtube/v3"); var request = new RestRequest("channels", Method.GET); request.AddParameter("part", "statistics"); request.AddParameter("id", channelId); request.AddParameter("key", apiKey); IRestResponse response = client.Execute(request); if (response.Data == null || response.Data.items == null || response.Data.items.Count == 0) { return null; } return response.Data.items.FirstOrDefault(); } public class ChannelDetailsResponse { public string kind { get; set; } public string etag { get; set; } public List items { get; set; } } public class ChannelDetails { public string kind { get; set; } public string etag { get; set; } public string id { get; set; } public ChannelStatistics statistics { get; set; } } public class ChannelStatistics { public string viewCount { get; set; } public string subscriberCount { get; set; } public string hiddenSubscriberCount { get; set; } public string videoCount { get; set; } }This code adds a new method,
GetChannelDetails, which fetches the channel statistics, including the subscriber count. TheMyOwnVideosmethod is updated to include the subscriber count in the HTML table.Thanks
Ramco RamcoRank #462 · 5.4K reputationPosted Feb 17, 2025, 6:53 AM EST
Hi Imran
Will it return no of subscribers each video wise or total no of subscribers. I want video wise subscribers
Thanks
Tuhin PaulRank #7 · 71.9K reputationPosted Feb 17, 2025, 4:01 PM EST
To get the subscriber count, you'll need to make an additional API call to the YouTube Data API's "channels" endpoint.
protected void MyOwnVideos() { StringBuilder htmlTable = new StringBuilder(); var client = new RestClient("googleapis.com/youtube/v3"); // First, get channel statistics var channelRequest = new RestRequest("channels", Method.GET); channelRequest.AddParameter("part", "statistics"); channelRequest.AddParameter("id", "UC_nNoaVw"); // Your channel ID channelRequest.AddParameter("key", "AIz8..."); // Your API key IRestResponse channelResponse = client.Execute(channelRequest); long subscriberCount = 0; if (channelResponse.Data?.items?.Any() == true) { subscriberCount = long.Parse(channelResponse.Data.items[0].statistics.subscriberCount); } // Get videos list var request = new RestRequest("search", Method.GET); request.AddParameter("part", "snippet"); request.AddParameter("type", "video"); request.AddParameter("channelId", "UC_nNoaVw"); request.AddParameter("key", "AIz8..."); IRestResponse response = client.Execute(request); if (response.Data == null || response.Data.items == null || response.Data.items.Count == 0) { Console.WriteLine("No Data Found. Please check your channelId and API key."); return; } // Build the HTML table with subscriber count htmlTable.Append(""); htmlTable.Append($"Channel Subscribers: {subscriberCount:N0}"); htmlTable.Append(" "); htmlTable.Append(""); htmlTable.Append("Video IdVideo TitleDescriptionPublished"); htmlTable.Append(""); foreach (var data in response.Data.items.OrderBy(x => x.snippet.publishedAt)) { htmlTable.Append(""); htmlTable.Append($"{data.id.videoId}"); htmlTable.Append($"{data.snippet.title}"); htmlTable.Append($"{data.snippet.description}"); htmlTable.Append($"{data.snippet.publishedAt}"); htmlTable.Append(""); } htmlTable.Append(""); htmlTable.Append(""); Console.WriteLine(htmlTable.ToString()); } // Add these new classes for channel statistics public class YoutubeChannelResponse { public List items { get; set; } } public class Channel { public string id { get; set; } public Statistics statistics { get; set; } } public class Statistics { public string viewCount { get; set; } public string subscriberCount { get; set; } public string hiddenSubscriberCount { get; set; } public string videoCount { get; set; } }Key changes made:
data.idtodata.id.videoId):N0format specifierThe subscriber count will now appear above the video table. Note that some channels might have their subscriber count hidden, in which case the
hiddenSubscriberCountproperty will be "true" andsubscriberCountwill not be available.To use this code, make sure you have: