2012-12-12 147 views
5

我想要在播放列表的YouTube上使用API​​ v3添加多個視頻,但只是其中一些添加...似乎問題是asset.position是隨機響應。API Youtube - 在YouTube播放列表中添加多個視頻

也許在兩個請求中有延遲?如果有人能幫助我?

代碼來重現問題:

var apiKey = '***'; 
    var youtubeClientId = '***'; 
    var youtubeScopes = [ 
    'https://www.googleapis.com/auth/youtube', 
    'https://www.googleapis.com/auth/youtube.upload', 
    'https://www.googleapis.com/auth/youtubepartner' 
    ]; 
    var playlistId = '***'; 

    function init() { 
    gapi.client.setApiKey(apiKey); 
    window.setTimeout(checkAuth,1); 
    } 

    function checkAuth() { 
     gapi.auth.authorize({client_id: youtubeClientId, scope: youtubeScopes, immediate: true}, handleAuthResult); 
    } 

    function handleAuthResult(authResult) { 
    var authorizeButton = document.getElementById('importYoutube'); 
    if (authResult && !authResult.error) { 
     gapi.client.load('youtube', 'v3', showSearch); 
     } 
    } 
    function showSearch() { 
    document.getElementById('search').style.display = ''; 
    } 


    function singleCallback(resp, rawResp) { 
    console && console.log(resp); 
    } 

    function writeResponse(resp) { 
    var infoDiv = document.getElementById('info'); 
    var result = resp.result; 
    if (!result.items) { return; } 

    var responseTitle = document.createElement('strong'); 
    responseTitle.innerHTML = 'Profiles results for ' + resp.id + ':'; 
    infoDiv.appendChild(responseTitle); 

    for (var i = 0; i < result.items.length ; i++) { 
     var profileResult = result.items[i]; 
     var profileInfo = document.createElement('P'); 

     if (profileResult.image && profileResult.image.url) { 
     var profilePic = document.createElement('IMG'); 
     profilePic.src = resizeImage(profileResult.image.url); 
     profileInfo.appendChild(profilePic); 
     } 

     var profileLink = document.createElement('A'); 
     profileLink.style.marginLeft = '5px'; 
     profileLink.href = profileResult.url; 
     profileLink.innerHTML = profileResult.displayName; 
     profileInfo.appendChild(profileLink); 

     infoDiv.appendChild(profileInfo); 
    } 
    } 

    function batchCallback(resp, rawResp) { 
    console.log(resp); 
    var infoDiv = document.getElementById('info'); 
    infoDiv.innerHTML = ''; 
    for (var id in resp) { 
     writeResponse(resp[id]); 
    } 
    } 

    function makeRequest() { 

    addToPlaylist('lRGoIiKXl8s'); 
    addToPlaylist('xvtNS6hbVy4'); 
    addToPlaylist('9I9Ar6upx34'); 
    addToPlaylist('Hr2Bc5qMhE4'); 
    addToPlaylist('ZaI2IlHwmgQ'); 
    addToPlaylist('fyMhvkC3A84'); 
    addToPlaylist('nv44r3q6zgo'); 

    } 


    function addToPlaylist(videoId, startPos, endPos) { 

    console.log('addToPlaylist videoId' + videoId); 
    console.log('addToPlaylist playlistId' + playlistId); 


    gapi.client.load('youtube', 'v3', function() { 

     var details = { 
     videoId: videoId, 
     kind: 'youtube#video' 
     } 

     if (startPos != undefined) { 
     details['startAt'] = startPos; 
     } 

     if (endPos != undefined) { 
     details['endAt'] = endPos; 
     } 


     var request = gapi.client.youtube.playlistItems.insert({ 
     part: 'snippet', 
     resource: { 
      snippet: { 
      playlistId: playlistId, 
      resourceId: details 
      } 
     } 
     }); 

     request.execute(function(response) { 
     console.log(JSON.stringify(response.result)); 
     }); 
    }); 
    } 

回答

相關問題