我目前正致力于通过AJAX将视频上传到我的频道。到目前为止,我已成功地对授权进行了排序,并且正在我的频道上创建“视频”,但这就是它结束的地方。没有数据似乎正在通过。我不确定如何正确发送元数据,以及实际视频数据本身是否正确发送。
以下是YouTube参考:https://developers.google.com/youtube/v3/docs/videos/insert
另一个奇怪的事情,请求设置为异步,我得不到响应,加载功能不运行。异步设置为false,我收到YouTube的回复。
function UploadVideo()
{
formData = new FormData();
video = {
snippet : {
title : "Test Video",
description : "This is a test video",
categoryId : "22",
tags : "test"
}
};
file = document.getElementById("test_video").files[0];
ajax = new XMLHttpRequest();
formData.append("file", file);
formData.append("snippet", JSON.stringify(video));
ajax.open("POST", "https://www.googleapis.com/upload/youtube/v3/videos?part=snippet", true);
ajax.setRequestHeader("Content-type", "application/octet-stream");
ajax.setRequestHeader("Authorization", '<?php echo $authorization_header; ?>');
ajax.onload = function() {
if (ajax.status == 200) {
alert(ajax.responseText);
document.getElementById("video_response").innerHTML = ajax.responseText;
}
};
ajax.send(formData);
}
答案 0 :(得分:0)
好吧,我没有弄清楚,但找到了一个整洁的解决方案。
所以它并不完美,但它实现了目标:通过带有元信息的AJAX上传新视频。
function UpdateVideoInfo(video_id){
var resource = {
'snippet':{
'title' : 'test title',
'description' : 'test description',
'categoryId' : 22
},
'status' : {
'privacyStatus' : 'private'
},
'id': video_id
};
post_string = JSON.stringify(resource);
var ajax = new XMLHttpRequest();
ajax.open('PUT', 'https://www.googleapis.com/youtube/v3/videos?part=snippet,status', true);
ajax.setRequestHeader("Authorization", '<?php echo $authorization_header; ?>');
ajax.setRequestHeader("Content-type", "application/json; charset=UTF-8");
ajax.send(post_string);
ajax.onload = function() {
if (ajax.status == 200) {
alert(ajax.responseText);
}
};
}
function UploadVideo(){
var file = document.getElementById("test_video").files[0];
var ajax = new XMLHttpRequest();
ajax.open("POST", "https://www.googleapis.com/upload/youtube/v3/videos?part=snippet", true);
ajax.setRequestHeader("Content-type", "application/octet-stream");
ajax.setRequestHeader("Authorization", '<?php echo $authorization_header; ?>');
ajax.onload = function() {
if (ajax.status == 200) {
var response_body = JSON.parse(ajax.responseText);
document.getElementById("video_response").innerHTML = response_body;
UpdateVideoInfo(response_body['id']);
}
};
ajax.send(file);
return false;
}