API данных Youtube с Javascript: прямая загрузка видео - PullRequest
0 голосов
/ 18 мая 2018

Благодаря документации API данных Youtube я могу выполнять возобновляемую загрузку видео с помощью Javascript (с XMLHttpRequest).Это хорошо задокументировано.Но есть функция Javascript (youtube.videos.insert), которая (с моей личной точки зрения) плохо документирована.После двух дней исследований в Интернете я вынужден обратиться за помощью сюда.

Вот пример кода

function defineRequest() {
if (!isAuthorized) {
  alert("You need to authorize the request to proceed.");
  return;
}

var file = document.querySelector('input[type=file]').files[0];
if (!file) {
    alert("You need to select a file to proceed.");
    return;
}

var reader = new FileReader();
reader.onloadend = function() {
    alert("file load completed");

    var data = reader.result;
    var fileStream = new Blob([new Uint8Array(data)], {type: 'application/octet-stream'});
    //var fileStream = new Blob([data], {type: 'application/octet-stream'});
    alert(fileStream.size);
    alert(fileStream.type);

    var request = gapi.client.youtube.videos.insert({
        media: {
            mimeType: 'application/octet-stream',
            body: fileStream
        },
        part: 'id,snippet,status',
        notifySubscribers: true,
        resource: {
            snippet: {
                categoryId: 22,
                title: $('#title').val(),
                description: $('#description').text(),
                tags: ['hello']
            },
            status: {
                privacyStatus: $('#privacy-status option:selected').text()
            }
        }
    });

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

        if (response === false) {
            alert("cannot upload video");
            console.log("Cannot upload video");
            return;
        } else {
            if (response.code != 0) {
                alert("cannot upload video");
                console.log("Cannot upload video");
                return;
            }

            var result = response.result;
            alert(JSON.stringify(result));
            alert("upload completed");          
        }
    });
}
reader.readAsArrayBuffer(file);

}

А вотошибку 400 я получил (mediaBodyRequired).Может у кого-нибудь есть подсказка?

{
"code":400,
"data":[{
        "domain":"youtube.video",
        "reason":"mediaBodyRequired",
        "message":"The request does not include the video content.",
        "locationType":"other",
        "location":"body"
    }],
"message":"The request does not include the video content.",
"error": {
    "code":400,
    "data":[{
        "domain":"youtube.video",
        "reason":"mediaBodyRequired",
        "message":"The request does not include the video content.",
        "locationType":"other",
        "location":"body"
    }],
    "message":"The request does not include the video content."
}

}

1 Ответ

0 голосов
/ 18 мая 2018

Вы читали документы ?Это выглядит совсем не так, как вы пишете.

...