C#
protected void MyOwnVideos11()
{
StringBuilder htmlTable = new StringBuilder();
string nextPageToken = string.Empty;
Int32 Sr = 1;
var client = new RestClient("googleapis.com/youtube/v3");
var request = new RestRequest("search", Method.GET);
request.AddParameter("type", "video");
request.AddParameter("channelId", "UC_nNoaVw);
request.AddParameter("key", "AIz8...");
if (!string.IsNullOrEmpty(nextPageToken))
{
request.AddParameter("pageToken", nextPageToken);
}
var response = client.Execute(request);
var orderByPublishAt = response.Data.items.OrderBy(x => x.snippet.publishedAt);
htmlTable.Append("");
htmlTable.Append("Sr.Video TitleDescriptionPublished");
htmlTable.Append("");
do
{
foreach (var data in orderByPublishAt)
{
var clientTag = new RestClient("googleapis.com/youtube/v3/");
var tagRequest = new RestRequest("videos", Method.GET);
tagRequest.AddParameter("key", "AIz8...");
tagRequest.AddParameter("part", "snippet,statistics");
tagRequest.AddParameter("id", data.id.videoId);
var tagResponse = clientTag.Execute(tagRequest);
foreach (var item in tagResponse.Data.items)
{
htmlTable.Append("" + Sr + "");
htmlTable.Append("" + item.snippet.title + "");
htmlTable.Append("" + item.snippet.description + "");
htmlTable.Append("" + data.snippet.publishedAt.ToString("dd-MM-yyyy") + "");
htmlTable.Append("");
}
Sr = Sr + 1;
nextPageToken = response.Data.nextPageToken;
}
} while (!string.IsNullOrEmpty(nextPageToken));
htmlTable.Append("");
htmlTable.Append("");
PlaceHolderTable.Controls.Add(new Literal { Text = htmlTable.ToString() });
}
Emily FosterRank #3,495 · 415 reputationPosted Mar 24, 2025, 9:38 PM EDT
It appears that the code you've provided is designed to fetch and display videos from the YouTube Data API in a tabular format.
The issue you are encountering with the code getting stuck in a loop might be due to how the `nextPageToken` is being handled.
In your code, you are assigning `nextPageToken = response.Data.nextPageToken` within the loop that iterates over the videos fetched in `orderByPublishAt`. This might cause the loop to keep fetching the same page of results, as `nextPageToken` is not being updated based on the actual next page token returned by the API response.
To resolve this issue, you should move the assignment of `nextPageToken = response.Data.nextPageToken` outside the inner `foreach` loop that iterates over the videos. Update `nextPageToken` after processing all videos on the current page, so that the loop proceeds to the next page in the subsequent iteration.
By making this adjustment, you should be able to traverse through all pages of results from the YouTube API without getting stuck in a loop fetching the same data repeatedly.
Let me know if you need further clarification or assistance with debugging the loop issue in your code!
Tuhin PaulRank #7 · 71.9K reputationPosted Mar 25, 2025, 6:08 AM EDT
The issue with your code lies in the way you handle the
nextPageTokenand the logic of thedo-whileloop. Specifically, the loop gets stuck because thenextPageTokenis updated within the innerforeachloop but is not properly reset or re-evaluated after processing all items for the current page.Incorrect
nextPageTokenHandling :nextPageTokenis updated inside theforeachloop (nextPageToken = response.Data.nextPageToken;), which is incorrect. It should be updated only after processing all items from the current page.do-whileloop keeps running indefinitely because the samenextPageTokenis reused repeatedly.Improper Loop Logic :
do-whileloop continues as long asnextPageTokenis not null or empty. However, since thenextPageTokenis not being updated correctly, the loop never terminates.Inefficient API Calls :
orderByPublishAtlist, you are making an additional API call to fetch tags/statistics (tagRequest). This results in excessive API calls, which can hit rate limits or slow down performance.Tuhin PaulRank #7 · 71.9K reputationPosted Mar 25, 2025, 6:10 AM EDT
To fix these issues, you need to:
nextPageTokenafter processing all items from the current page.do-whileloop terminates when there are no more pages to process.protected void MyOwnVideos11() { StringBuilder htmlTable = new StringBuilder(); string nextPageToken = string.Empty; Int32 Sr = 1; var client = new RestClient("https://www.googleapis.com/youtube/v3"); htmlTable.Append(""); htmlTable.Append("Sr.Video TitleDescriptionPublished"); htmlTable.Append(""); do { var request = new RestRequest("search", Method.GET); request.AddParameter("type", "video"); request.AddParameter("channelId", "UC_nNoaVw"); // Replace with your channel ID request.AddParameter("key", "AIz8..."); // Replace with your API key request.AddParameter("part", "snippet"); request.AddParameter("maxResults", "50"); // Max results per page (YouTube API limit) if (!string.IsNullOrEmpty(nextPageToken)) { request.AddParameter("pageToken", nextPageToken); } var response = client.Execute(request); if (response.Data == null || response.Data.items == null) { break; // Exit if no data is returned } var orderByPublishAt = response.Data.items.OrderBy(x => x.snippet.publishedAt); foreach (var data in orderByPublishAt) { var clientTag = new RestClient("https://www.googleapis.com/youtube/v3"); var tagRequest = new RestRequest("videos", Method.GET); tagRequest.AddParameter("key", "AIz8..."); // Replace with your API key tagRequest.AddParameter("part", "snippet,statistics"); tagRequest.AddParameter("id", data.id.videoId); var tagResponse = clientTag.Execute(tagRequest); if (tagResponse.Data == null || tagResponse.Data.items == null) { continue; // Skip if no data is returned } foreach (var item in tagResponse.Data.items) { htmlTable.Append(""); htmlTable.Append("" + Sr + ""); htmlTable.Append("" + item.snippet.title + ""); htmlTable.Append("" + item.snippet.description + ""); htmlTable.Append("" + data.snippet.publishedAt.ToString("dd-MM-yyyy") + ""); htmlTable.Append(""); Sr++; } } // Update nextPageToken after processing all items from the current page nextPageToken = response.Data.nextPageToken; } while (!string.IsNullOrEmpty(nextPageToken)); htmlTable.Append(""); htmlTable.Append(""); PlaceHolderTable.Controls.Add(new Literal { Text = htmlTable.ToString() }); }Ramco RamcoRank #462 · 5.4K reputationPosted Mar 25, 2025, 9:45 PM EDT
Hi Tuhin
It still stuck in loop. Not displaying any video
Thanks