Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

API YouTube - Adding multiple videos on YouTube playlist

I want to add multiple videos on a YouTube playlist using API v3, but just some of them are added... It seems that the problem is the asset.position that is random in the response.

Maybe have a delay in two requests? How can I fix this?

Code to reproduce the problem:

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));
        });
    });
}
like image 493
MatthieuH Avatar asked Nov 13 '22 15:11

MatthieuH


1 Answers

I also had the same problem (see Can not insert multiple videos into a playlist - YouTube API v3).

You will have to create a delay after every call to addToPlaylist().

like image 118
Rohan Avatar answered Nov 15 '22 04:11

Rohan