我很好被授权。
我的页面上有2个这样的按钮:
<input type="file" id="fileToSend"/>
<input type="button" onclick="upload()" value="Upload" id="btnSend"/>
点击“上传”按钮后,我想将所选文件上传到youtube。我正在调用这样的函数:
function upload() {
var fileStream;
var video = document.getElementById("fileToSend");
var file = video.files[0];
console.log(file);
console.log("Nombre: " + file.name);
var r = new FileReader();
r.onload = function () {
console.log("fileStream creado");
fileStream = r.result;
//console.log("FileStream: " + fileStream);
};
console.log("Creando fileStream..");
r.readAsBinaryString(file);
gapi.client.load('youtube', 'v3',
function() {
var request = gapi.client.youtube.videos.insert({
part: 'snippet, status',
resource: {
snippet: {
title: 'Video Test Title 5',
description: 'Video Test Description',
tags: ['Tag 1', 'Tag 2'],
categoryId: "22"
},
status: {
privacyStatus: "private"
}
}
}, fileStream);
request.execute(function (response) {
console.log("executing..");
var result = response.result;
console.log(response);
if (result) {
console.log("execute completed");
document.write(result);
}
});
});
}
问题是我在响应对象上遇到了错误,“mediaBodyRequired”,这就像我没有正确发送fileStream。
答案 0 :(得分:4)
您是否有理由不能使用YouTube上传小工具?是https://developers.google.com/youtube/youtube_upload_widget
无论如何,直接来自API参考
https://developers.google.com/youtube/v3/docs/videos/insert
badRequest mediaBodyRequired The request does not include the video content.
另一种资源:
https://developers.google.com/api-client-library/javascript/samples/samples
答案 1 :(得分:1)
使用v3插入有两种选择。请求必须:
我从未使用官方JavaScript客户端进行此工作,但是使用常规XmlHttpRequest编写了一个非常详细的解释:http://lithostech.com/2013/10/upload-google-youtube-api-v3-cors/
以下是第一种方法的示例,其中文件本身是整个请求正文:
// where videoFile is a http://www.w3.org/TR/FileAPI/#dfn-file
var invocation = new XMLHttpRequest();
invocation.setRequestHeader('Authorization', 'Bearer ' + token);
invocation.open('POST', "https://www.googleapis.com/upload/youtube/v3/videos?part=snippet", true);
invocation.send(videoFile);