Hi
In below code i want that all Videos should get displayed. It displays only 50 Videos.
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 Id Video Title Description Published ");
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());
}
Thanks
Tuhin PaulPosted Mar 23, 2025, 6:30 PM
Pagination with
pageToken:nextPageTokenin the response when there are more results to fetch.pageTokenparameter.nextPageTokenis null).Dynamic Table Building:
Handling
videoId:idfield in the API response is an object containingvideoId. Ensure you access it correctly usingdata.id.videoId.Error Handling:
Max Results Per Page:
maxResultsparameter is explicitly set to 50 (the maximum allowed per request). You can reduce this if needed.Tuhin PaulPosted Mar 23, 2025, 6:29 PM
The issue in your code is that the YouTube Data API v3 limits the number of results returned in a single request to 50 items by default. To retrieve more than 50 videos, you need to use pagination with the
pageTokenparameter. The API provides anextPageTokenin the response, which you can use to fetch the next set of results.Muhammad Imran AnsariPosted Mar 23, 2025, 11:44 AM
Hello Ramco,
By default, the YouTube Data API returns only 50 results per request. To fetch all videos, you need to handle pagination using the
nextPageToken. Modify your code to loop through all pages until there are no more results. Here is an updated code snippet:God Luck!
Jignesh KumarPosted Mar 23, 2025, 10:32 AM
Hello Ramco,
You can use nextpageToken which you get from youtube API response, you need to use do --- While loop to get all videos from API response, Please use below code to get all videos.
Emily FosterPosted Mar 23, 2025, 5:29 AM
Hey there! It seems like you're looking to display all videos from a specific YouTube channel in your code snippet. Currently, you're retrieving videos by using the YouTube Data API but only displaying 50 videos due to the default limit.
To display all videos, you can adjust your code to handle pagination and retrieve all available videos. Here are a few steps to consider:
1. Pagination: The YouTube Data API uses pagination to limit the number of results per page. You can make additional requests to get the next page of results until you have all the videos.
2. Update Request: Modify your existing code to handle pagination. You may need to make subsequent requests using the `pageToken` parameter to fetch the next set of results.
3. Loop through Pages: Implement a loop to continue fetching videos until there are no more pages left.
Here's a high-level overview of what you could do:
- Keep track of the `pageToken` returned in the API response.
- Make subsequent requests by adding the `pageToken` parameter with the value obtained from the previous response.
- Continue this process until you've fetched all the videos.
By incorporating pagination handling in your code, you will be able to retrieve and display all videos from the specified YouTube channel. If you need further assistance with the implementation or have any specific questions, feel free to ask!