The latest YouTube video does not load, previously it did

Recently, this same API worked for me by directing me to the most recent video on the corresponding YouTube channel. However, now when I click the button, it takes me to a video from 2 months ago instead of the latest one. I don’t understand what happened; I didn’t make any changes, and it has stopped working as it did before. I’m attaching the code to see if someone could help me.

async function getLastVideo() {
    const url = `https://www.googleapis.com/youtube/v3/search?key=${apiKey}&channelId=${channelId}&order=date&part=snippet&type=video&maxResults=1`;

    try {
        const response = await fetch(url);
        const data = await response.json();
        if (data.items.length > 0) {
            const lastVideoId = data.items[0].id.videoId;
            const lastVideoUrl = `https://www.youtube.com/watch?v=${lastVideoId}`;

            // Actualiza los enlaces con los IDs únicos
            document.getElementById('youtube-last-video-1').href = lastVideoUrl;
            document.getElementById('youtube-last-video-2').href = lastVideoUrl;
            document.getElementById('youtube-last-video-3').href = lastVideoUrl;
        } else {
            console.error('No se encontraron videos.');
        }
    } catch (error) {
        console.error('Error al obtener el último video:', error);
    }
}

// Llama a la función cuando la página cargue
window.onload = getLastVideo;

I tried a few different approaches to fix the issue, though I don’t recall all the details. I attempted to:

  • Check the API documentation to ensure I was using it correctly.
  • Verify the API key and endpoint for any potential issues.
  • Clear the cache and refresh the page to rule out any caching problems.
  • Review recent changes in my code or environment that might have affected the API’s behavior.

I was expecting the button to direct me to the most recent video on the YouTube channel, as it did before. Despite these attempts, the issue persists.