我有類似以下內(nèi)容:const BASE_URL = 'https://www.googleapis.com/youtube/v3';/** * @param {string} channelId * @param {string?} pageToken */const getPlaylist = (channelId, pageToken) => { const url = `${BASE_URL}/playlists?part=snippet&channelId=${channelId}&key=${API_KEY}&part=id,snippet&maxResults=50`; return pageToken ? `${url}&pageToken=${pageToken}` : url;};/** * @param {string} playlistId * @param {string?} pageToken */const getPlaylistItems = (playlistId, pageToken) => { const url = `${BASE_URL}/playlistItems?part=snippet&playlistId=${playlistId}&key=${API_KEY}&part=id,snippet&maxResults=50`; return pageToken ? `${url}&pageToken=${pageToken}` : url;}const getVideo = videoId => `${BASE_URL}/vidoes?part=statistics&id=${videoId}&key=${API_KEY}`;const fetchViewCountByVideoId = async videoId => { const response = await fetch(getVideo(videoId)); const { statistics: { viewCount } } = await response.json(); return viewCount;};所以我試圖獲取頻道的播放列表,獲取每個(gè)播放列表中的所有播放列表項(xiàng)目,獲取每個(gè)項(xiàng)目的觀看次數(shù)。(我省略了其余的代碼,因?yàn)樗ぷ髡?。)問題是,前兩個(gè) API 批量調(diào)用工作正常,但是當(dāng)我訪問 API 時(shí)videos,會(huì)拋出 CORS 錯(cuò)誤。(我在瀏覽器控制臺(tái)中運(yùn)行代碼,因?yàn)槭褂么舜a的人不是程序員,而且我不想花時(shí)間創(chuàng)建網(wǎng)頁并托管它。)問題我是否需要登錄 API 調(diào)用才能正常工作(并使用標(biāo)authorization頭),或者 API 的響應(yīng)是否存在videos錯(cuò)誤?
具有瀏覽器獲取功能的 Youtube v3 API 會(huì)引發(fā)“視頻”的 CORS 錯(cuò)誤
ITMISS
2023-07-20 16:30:31