0

我想讓網絡客戶端可以將視頻添加到播放列表中。目前我不知道什麼是錯誤的,所以幫助是比歡迎。Youtube播放列表插入API Javascript

我得到錯誤代碼400()並檢查了錯誤代碼,但有一點可以確定視頻不在播放列表中。同樣的確切的東西在他們的「嘗試它!」中起作用部分。所以我認爲我的配置變量不正確或即時發送錯誤的方式。

function addSongYTPlaylist(){ 
    var xmlhttp = new XMLHttpRequest(); 
    var url = "https://www.googleapis.com/youtube/v3/playlistItems?part=snippet&part=snippet&key=AIzaSyA17v8PuNBsIwgbxg6D78iHV-w7_dYyXPw"; 

    var config = { 
    "snippet": { 
     "playlistId": {PLAYLIST_ID}, 
     "resourceId": { 
     "videoId": {VIDEO_ID}, 
     "kind": "youtube#video" 
     } 
    } 
    } 

    xmlhttp.onreadystatechange = function() { 
     if (xmlhttp.readyState == 4 && xmlhttp.status == 200) { 
      var response = JSON.parse(xmlhttp.responseText); 
      console.log(response); 
     } 
    }; 

    console.log(url); 
    xmlhttp.open("POST", url, true); 
    xmlhttp.setRequestHeader("Authorization", "Bearer {ACCESS_TOKEN}); 
    xmlhttp.send(config); 
} 

回答

0

從這個documentation,你可以找到不同的錯誤代碼和他們的YouTube API說明。

您得到的錯誤代碼400 badRequest意味着您的請求缺少必需的參數。並且您收到的消息是「未選擇過濾器。預期的播放列表ID,ID」

因此,您需要提供playlistId或Id才能使其工作。

https://www.googleapis.com/youtube/v3/playlistItems?part=snippet&playlistId=PL889BDECC517358C9&key=AIzaSyA17v8PuNBsIwgbxg6D78iHV-w7_dYyXPw 

也爲更多的信息,您可以檢查PlaylistItems的documention:插入。