403 Error while interacting with the third-party API source (YouTube)

I’ve made this small web app that shows YouTube account data. The key component of the application is interacting with the YouTube API using the key I’ve generated on https://console.cloud.google.com/. The app worked perfectly for about a day; I tested it multiple times. Suddenly, I started receiving the 403 error just as I clicked the button that started interacting with the third-party source (YouTube API).

Here is the JavaScript code that I’m using:

const API_KEY = 'AIzaSyD8Xi7C_6dow9y11i20ZQWnlC12b17FlXc';  // Replace with your actual API key

const userAgents = [
    'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.124 Safari/537.36',
    'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/14.1.1 Safari/605.1.15',
    'Mozilla/5.0 (iPhone; CPU iPhone OS 14_6 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/14.0 Mobile/15E148 Safari/604.1',
    'Mozilla/5.0 (Linux; Android 11; SM-G991U) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.124 Mobile Safari/537.36',
    // Add more user agents as needed
];

function getRandomUserAgent() {
    return userAgents[Math.floor(Math.random() * userAgents.length)];
}

async function fetchYouTubeData() {
    let input = document.getElementById('channelId').value.trim();
    if (!input) {
        alert('Please enter a YouTube Channel ID or Username');
        return;
    }

    let channelId = input;

    if (input.startsWith('@')) {
        // If the input starts with @, it's a username
        const username = input.substring(1);
        try {
            const searchResponse = await fetch(`https://www.googleapis.com/youtube/v3/search?part=id&type=channel&q=${username}&key=${API_KEY}`, {
                headers: {
                    'User-Agent': getRandomUserAgent(),
                    'Referer': 'https://www.youtube.com/',
                    'Origin': 'https://www.youtube.com'
                }
            });
            if (!searchResponse.ok) {
                throw new Error(`HTTP error! status: ${searchResponse.status}`);
            }
            const searchData = await searchResponse.json();
            if (!searchData.items || searchData.items.length === 0) {
                alert('No channel found for this username');
                return;
            }
            channelId = searchData.items[0].id.channelId;  // Extract the channel ID directly
        } catch (error) {
            console.error('Error fetching YouTube data:', error);
            alert(`Failed to fetch YouTube data: ${error.message}`);
            return;
        }
    }

    try {
        // Fetch channel information
        const channelResponse = await fetch(`https://www.googleapis.com/youtube/v3/channels?part=snippet,contentDetails,statistics&id=${channelId}&key=${API_KEY}`, {
            headers: {
                'User-Agent': getRandomUserAgent(),
                'Referer': 'https://www.youtube.com/',
                'Origin': 'https://www.youtube.com'
            }
        });
        if (!channelResponse.ok) {
            throw new Error(`HTTP error! status: ${channelResponse.status}`);
        }
        const channelData = await channelResponse.json();

        if (!channelData.items || channelData.items.length === 0) {
            alert('No data found for this Channel ID');
            return;
        }

        const channel = channelData.items[0];

        // Fetch latest video from the channel
        const videoResponse = await fetch(`https://www.googleapis.com/youtube/v3/search?part=snippet&channelId=${channelId}&type=video&order=date&maxResults=1&key=${API_KEY}`, {
            headers: {
                'User-Agent': getRandomUserAgent(),
                'Referer': 'https://www.youtube.com/',
                'Origin': 'https://www.youtube.com'
            }
        });
        if (!videoResponse.ok) {
            throw new Error(`HTTP error! status: ${videoResponse.status}`);
        }
        const videoData = await videoResponse.json();

        if (!videoData.items || videoData.items.length === 0) {
            alert('No videos found for this Channel');
            return;
        }

        const latestVideo = videoData.items[0];
        const videoInfo = {
            title: latestVideo.snippet.title,
            publishedAt: new Date(latestVideo.snippet.publishedAt).toLocaleDateString(),
            thumbnail: latestVideo.snippet.thumbnails.medium.url
        };

        // Display channel and video information
        const channelInfo = `
            <div>
                <img src="${channel.snippet.thumbnails.medium.url}" alt="Channel Thumbnail">
                <div>
                    <h2>${channel.snippet.title}</h2>
                    <p><strong>Description:</strong> ${channel.snippet.description}</p>
                    <p><strong>Total Views:</strong> ${channel.statistics.viewCount}</p>
                    <p><strong>Number of Videos:</strong> ${channel.statistics.videoCount}</p>
                    <p><strong>Subscribers:</strong> ${channel.statistics.subscriberCount}</p>
                    <p><strong>Last Video Published At:</strong> ${videoInfo.publishedAt}</p>
                    <p><strong>Latest Video:</strong> ${videoInfo.title}</p>
                    <img src="${videoInfo.thumbnail}" alt="Latest Video Thumbnail">
                </div>
            </div>
        `;
        document.getElementById('channelData').innerHTML = channelInfo;
    } catch (error) {
        console.error('Error fetching YouTube data:', error);
        alert(`Failed to fetch YouTube data: ${error.message}`);
    }
}
body {
    font-family: 'Times New Roman', Times, serif;
    background-color: #f9f9f9;
    margin: 0;
    padding: 0;
    display: flex;
    justify-content: center;
    align-items: center;
    height: 100vh;
}

.container {
    max-width: 600px;
    padding: 20px;
    background-color: #fff;
    box-shadow: 0 0 10px rgba(0, 0, 0, 0.1);
    text-align: center;
}

.title {
    font-size: 28px;
    line-height: 1.2;
    margin-bottom: 20px;
}

.subtitle-container {
    position: relative;
}

.subtitle {
    font-size: 16px;
    color: transparent;
    background-image: linear-gradient(to right, violet, indigo, blue, green, yellow, orange, red);
    -webkit-background-clip: text;
    margin-bottom: 20px;
    display: inline-block;
    padding: 10px 20px;
    border-radius: 4px;
    text-align: center;
    background-color: #333; /* Dark grey background color */
    width: fit-content; /* Ensures the background spans only as wide as the text */
    margin: 0 auto; /* Centers the subtitle */
}

.line {
    width: 80%;
    height: 2px;
    background-color: #333;
    margin: 0 auto 20px auto; /* Centered and spaced below subtitle */
}

.search-container {
    margin-bottom: 40px;
}

input[type="text"] {
    padding: 10px;
    width: 60%;
    margin-right: 10px;
    font-family: 'Times New Roman', Times, serif;
    border: 1px solid #ccc;
    border-radius: 4px;
}

button {
    padding: 10px 20px;
    font-size: 16px;
    font-family: 'Times New Roman', Times, serif;
    border: none;
    background-color: #007bff;
    color: white;
    cursor: pointer;
    border-radius: 4px;
}

button:hover {
    background-color: #0056b3;
}

.channel-info {
    margin-top: 40px;
    text-align: left;
}

.channel-info img {
    max-width: 100px;
    border-radius: 50%;
    margin-right: 20px;
    float: left;
}
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Peter's YouTube Data Checker</title>
    <link rel="stylesheet" href="styles.css">
</head>
<body>
    <div class="container">
        <h1 class="title">Peter's<br>YouTube<br>Data Checker</h1>
        <div class="subtitle-container">
            <p class="subtitle">Check every YouTube channel's most recent data fast and easy</p>
            <div class="line"></div>
        </div>
        <div class="search-container">
            <input type="text" id="channelId" placeholder="Enter YouTube Channel ID">
            <button onclick="fetchYouTubeData()">Get Channel Info</button>
        </div>
        <div id="channelData" class="channel-info"></div>
    </div>

    <script src="script.js"></script>
</body>
</html>

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật