Youtube API,Javascript-上传视频
我需要用Javascript从我的web应用程序上传一个视频到youtube。我尝试了两种方法:使用官方API和CORS。他们都没有工作 解决方案1:使用他们的APIYoutube API,Javascript-上传视频,javascript,youtube-api,youtube-data-api,Javascript,Youtube Api,Youtube Data Api,我需要用Javascript从我的web应用程序上传一个视频到youtube。我尝试了两种方法:使用官方API和CORS。他们都没有工作 解决方案1:使用他们的API uploadVideo(infos: any, file: File) { const params: any = { part: 'id,snippet,status', media: { body: null }, resource: { snippet: {
uploadVideo(infos: any, file: File) {
const params: any = {
part: 'id,snippet,status',
media: {
body: null
},
resource: {
snippet: {
title: infos.title
},
status: {}
},
fileSize: file.size
};
const fileReader = new FileReader();
const optSnippetParams = ['description', 'categoryId', 'defaultLanguage'];
const optStatusParams = ['publishAt', 'privacyStatus', 'license', 'publicStatsViewable'];
for (const i of optSnippetParams) {
if (infos[i]) {
params.resource.snippet[i] = infos[i];
}
}
for (const i of optStatusParams) {
if (infos[i]) {
params.resource.status[i] = infos[i];
}
}
if (infos.tags) {
params.resource.snippet.tags = infos.tags;
}
fileReader.onloadend = (e: ProgressEvent) => {
params.media.body = fileReader.result;
gapi.client.youtube.videos.insert(params).then((res: any) => {
console.log(res);
});
};
fileReader.readAsArrayBuffer(file);
}
此解决方案不起作用,我有以下错误:
{
"domain": "youtube.video",
"location": "body",
"locationType": "other",
"message": "The request does not include the video content.",
"reason": "mediaBodyRequired"
}
{
domain: "global",
message: "Media type 'application/json' is not supported. Valid media types: [video/*, application/octet-stream]"
reason: "badContent"
}
其中视频内容实际上是在请求主体中设置的
解决方案2:使用CORS(我从和中包含的示例中获得灵感)
这段代码中的第一件事是将'Content-Type'
设置为'application/json'
不起作用。我有以下错误:
{
"domain": "youtube.video",
"location": "body",
"locationType": "other",
"message": "The request does not include the video content.",
"reason": "mediaBodyRequired"
}
{
domain: "global",
message: "Media type 'application/json' is not supported. Valid media types: [video/*, application/octet-stream]"
reason: "badContent"
}
然而,这是第131行所使用的,所以我选择使用文件类型,这是有效的
第二件事是,我需要在POST
请求成功后执行PUT
请求以上载文件。为此,我需要使用POST
request'Location'
标题。然而,当我尝试获取它时,结果总是null
。所以我不能继续做任何事情
解决这个问题的方法是什么?找到了解决方法吗?找到了解决方法吗?