Javascript API YouTube-在YouTube播放列表中添加多个视频

Javascript API YouTube-在YouTube播放列表中添加多个视频,javascript,youtube-api,Javascript,Youtube Api,我想使用API v3在YouTube播放列表中添加多个视频,但只添加了其中一些。。。问题似乎在于响应中随机的asset.position 可能有两个请求延迟?我怎样才能解决这个问题 重现问题的代码: var apiKey = '***'; var youtubeClientId = '***'; var youtubeScopes = [ 'https://www.googleapis.com/auth/youtube', 'https://www.googlea

我想使用API v3在YouTube播放列表中添加多个视频,但只添加了其中一些。。。问题似乎在于响应中随机的asset.position

可能有两个请求延迟?我怎样才能解决这个问题

重现问题的代码:

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));
        });
    });
}
var apiKey='***';
var youtubeClientId=“***”;
变量youtubeScopes=[
'https://www.googleapis.com/auth/youtube',
'https://www.googleapis.com/auth/youtube.upload',
'https://www.googleapis.com/auth/youtubepartner'
];
变量playlaid='***';
函数init(){
gapi.client.setApiKey(apiKey);
setTimeout(checkAuth,1);
}
函数checkAuth(){
gapi.auth.authorize({client_id:youtubeClientId,scope:youtubeScopes,immediate:true},handleAuthResult);
}
函数handleAuthResult(authResult){
var authorizeButton=document.getElementById('importYoutube');
if(authResult&!authResult.error){
load('youtube','v3',showSearch);
}
}
函数showSearch(){
document.getElementById('search').style.display='';
}
函数singleCallback(resp,rawrep){
控制台和控制台日志(resp);
}
函数writeResponse(resp){
var infoDiv=document.getElementById('info');
var结果=相应结果;
如果(!result.items){
返回;
}
var responseTitle=document.createElement('strong');
responseTitle.innerHTML='Profiles resp.id+'的结果:';
信息部附属儿童(负责人);
对于(变量i=0;i
我也有同样的问题(请参阅)

每次调用addToPlaylist()后,都必须创建延迟