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 Title Description Published ");
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() });
} Loading
Ramco RamcoPosted Mar 26, 2025, 1:45 AM
Hi Tuhin
It still stuck in loop. Not displaying any video
Thanks
Tuhin PaulPosted Mar 25, 2025, 10:10 AM
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.Tuhin PaulPosted Mar 25, 2025, 10:08 AM
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.Emily FosterPosted Mar 25, 2025, 1:38 AM
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!