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
Daniel WrightRank #2,947 · 489 reputationPosted Feb 25, 2025, 8:33 PM EST
Hi! It seems like you're working on retrieving data about your own videos from YouTube's API. To list the number of subscribers for each video, you would typically need to make additional API calls to fetch that specific information.
Here's an overview of the steps you could take to include the number of subscribers along with the other video details:
1. Extend the `Snippet` class to include a property for the number of subscribers.
2. Modify your API request to fetch the subscriber count along with other video details.
3. Update your HTML table generation to display the subscriber count for each video.
For step 1, you can enhance the `Snippet` class like this:
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 int subscriberCount { get; set; } // New property for subscriber count }For step 2, you would need to adjust your API call to include fetching the subscriber count. This might necessitate a different endpoint or an additional API call to get that specific information.
Finally, in step 3, once you have the subscriber count in the `Snippet` object, you can include it in your HTML table generation within the foreach loop. For example:
htmlTable.Append($"<td>{data.snippet.subscriberCount}</td>");Remember that fetching subscriber count might require additional permissions or tokens from the YouTube API, so make sure to handle that accordingly.
If you encounter any challenges or have more specific requirements, feel free to ask for further assistance. Good luck with your YouTube API integration!
Tuhin PaulRank #7 · 71.9K reputationPosted Feb 28, 2025, 2:55 AM EST
To include the number of subscribers for each video's channel in your code, you need to make an additional API call to the YouTube Data API. Specifically, you can use the
channelsendpoint with thestatisticspart to retrieve the subscriber count for the channel associated with each video.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; } // 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)) { // Get the subscriber count for the channel string channelId = data.snippet.channelId; int subscriberCount = GetSubscriberCount(channelId, client); 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()); } // Helper method to get the subscriber count for a given channelId private int GetSubscriberCount(string channelId, RestClient client) { var request = new RestRequest("channels", Method.GET); request.AddParameter("part", "statistics"); request.AddParameter("id", channelId); request.AddParameter("key", "AIz8..."); // Replace with your actual API key IRestResponse response = client.Execute(request); if (response.Data == null || response.Data.items == null || response.Data.items.Count == 0) { return 0; // Return 0 if no data is found } // Extract the subscriber count from the response return int.Parse(response.Data.items[0].statistics.subscriberCount); } // Classes for deserializing the YouTube API responses 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; } } public class ChannelResponse { public List items { get; set; } } public class ChannelItem { public string kind { get; set; } public string etag { get; set; } public string id { get; set; } public Statistics statistics { get; set; } } public class Statistics { public string viewCount { get; set; } public string subscriberCount { get; set; } public bool hiddenSubscriberCount { get; set; } public string videoCount { get; set; } }Tuhin PaulRank #7 · 71.9K reputationPosted Feb 28, 2025, 2:56 AM EST
Added a New Column for Subscribers :
section of the HTML table now includes a new column:- Each row in the
Subscribers .section includes the subscriber count for the respective channel.-
- A new method
- This method makes a separate API call for each video's channel and retrieves the
-
- Inside the loop that processes each video, the
- The subscriber count is then appended to the HTML table.
-
- Added
- The
- For each video, it extracts the
- The subscriber count is displayed in the HTML table alongside other video details.

-
- Each API call to the YouTube Data API consumes quota. Since this code makes an additional API call (
-
- The
-
- Some channels may hide their subscriber counts. In such cases, the
-
- Replace
Know the answer? Forums 01
Arun PottiAnswers in the forums20answers02
Tahir AnsariAnswers in the forums5answers03
Kamlesh KumarAnswers in the forums5answers04
Sam HobbsAnswers in the forums4answers05
Željko PerićAnswers in the forums3answers?YouYour name goes here.Every answer: 20 SHARP.Join Full leaderboard → 2026
DevSocialUpcoming · 3rd editionAI Agents ConferenceDec 1–3, 2026Live onlineFree to attend5days leftRegisterHelper Method to Fetch Subscriber Count :
GetSubscriberCountwas added to fetch the subscriber count for a givenchannelIdusing thechannelsendpoint of the YouTube Data API.subscriberCountfrom thestatisticspart of the response.Updated the Loop :
GetSubscriberCountmethod is called to fetch the subscriber count for the channel associated with the video.New Classes for Deserializing the
channelsEndpoint Response :ChannelResponse,ChannelItem, andStatisticsclasses to handle the response from thechannelsendpoint.MyOwnVideosmethod fetches the list of videos using thesearchendpoint.channelIdand calls theGetSubscriberCountmethod to fetch the subscriber count for the channel.Output Example
Video Id
Video Title
Description
Published
Subscribers
dQw4w9WgXcQ
Example Video Title
This is a test desc
2023-01-01T12:00:00
1,234,567
abc123xyz
Another Video
Another description
2023-02-15T15:30:00
987,654
Tuhin PaulRank #7 · 71.9K reputationPosted Feb 28, 2025, 2:56 AM EST
API Quota :
channels) for each video, ensure you have sufficient quota or optimize the calls (e.g., batch requests if possible).Error Handling :
GetSubscriberCountmethod returns0if no data is found for a channel. You can enhance error handling as needed.Hidden Subscriber Counts :
hiddenSubscriberCountfield will betrue, and thesubscriberCountwill not be available.API Key :
"AIz8..."with your actual YouTube Data API key.Answer a question in the forums and earn +20 SHARP for every answer.
See unanswered questionsTop experts