Download YouTube videos from a browser

Im OK authorized.

I have 2 buttons on this page:

<input type="file" id="fileToSend"/> <input type="button" onclick="upload()" value="Upload" id="btnSend"/> 

I want to upload the selected file to youtube when I click the "Download" button. Im calling this function:

 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); } }); }); } 

The problem is that I get al error in the response object, "mediaBodyRequired", it seems like I am not sending fileStream correctly.

+8
javascript upload youtube-api youtube-javascript-api
source share
2 answers

Is there a reason you can't just use the YouTube upload widget?
https://developers.google.com/youtube/youtube_upload_widget

In any case, directly from the API link
https://developers.google.com/youtube/v3/docs/videos/insert

 badRequest mediaBodyRequired The request does not include the video content. 

Another resource:
https://developers.google.com/api-client-library/javascript/samples/samples

+3
source share

There are two options for using insert v3. The request must either:

  • has a media file as a body, excluding the sending of any other POST parameters, or
  • use multi-page coding in two parts. One part is the file to download, and the other part is a block JSON file that includes any parameters that you want to send.

I never got this working using the official JavaScript client, but wrote a fairly detailed explanation of how this can work using the usual XmlHttpRequest: http://lithostech.com/2013/10/upload-google-youtube-api-v3 -cors /

Here is an example of the first method in which the file itself is an entire request object:

 // 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); 
+1
source share

All Articles