Note that there are some explanatory texts on larger screens.

plurals
  1. PONode.js YouTube API Upload unable to convert video
    text
    copied!<p>I'm trying to upload video to youtube programatically. I chose to use Node.js for the task.</p> <p>I get an XML response as well as an HTTP Status Code of 201 and I see the video appear in video manager, however the video always has the message "Failed (unable to convert video file)".</p> <p>I can upload the file through YouTube's own uploader on their page and there are no problems. I only have to upload to a single account, so I set up the OAuth2 for the account and stored the refresh token. The refresh token is hard-coded, though I replace it with a variable below.</p> <p>Does the refresh token need to, itself, be refreshed?</p> <p>My code:</p> <pre><code>var qs = require('querystring'), https = require('https'), fs = require('fs'); var p_data = qs.stringify({ client_id: myClientID, client_secret: myClientSecret, refresh_token: refreshTokenForAccount, grant_type: 'refresh_token' }); var p_options = { host: 'accounts.google.com', port: '443', method: 'POST', path: '/o/oauth2/token', headers: { 'Content-Type': 'application/x-www-form-urlencoded', 'Content-Length': p_data.length, 'X-GData-Key': myDeveloperKey } }; var file_path = process.argv[1] || "video.mp4"; var json = ""; var p_req = https.request(p_options, function(resp){ resp.setEncoding( 'utf8' ); resp.on('data', function( chunk ){ json += chunk; }); resp.on("end", function(){ debugger; var access_token = JSON.parse(json).access_token; var title="test upload1", description="Second attempt at an API video upload", keywords="", category="Comedy"; var file_reader = fs.createReadStream(file_path, {encoding: 'binary'}); var file_contents = ''; file_reader.on('data', function(data) { file_contents += data; }); file_reader.on('end', function() { var xml = '&lt;?xml version="1.0"?&gt;' + '&lt;entry xmlns="http://www.w3.org/2005/Atom" xmlns:media="http://search.yahoo.com/mrss/" xmlns:yt="http://gdata.youtube.com/schemas/2007"&gt;' + ' &lt;media:group&gt;' + ' &lt;media:title type="plain"&gt;' + title + '&lt;/media:title&gt;' + ' &lt;media:description type="plain"&gt;' + description + '&lt;/media:description&gt;' + ' &lt;media:category scheme="http://gdata.youtube.com/schemas/2007/categories.cat"&gt;' + category + '&lt;/media:category&gt;' + ' &lt;media:keywords&gt;' + keywords + '&lt;/media:keywords&gt;' + ' &lt;/media:group&gt;' + '&lt;/entry&gt;'; var boundary = Math.random(); var post_data = []; var part = ''; part = "--" + boundary + "\r\nContent-Type: application/atom+xml; charset=UTF-8\r\n\r\n" + xml + "\r\n"; post_data.push(new Buffer(part, "utf8")); part = "--" + boundary + "\r\nContent-Type: video/mp4\r\nContent-Transfer-Encoding: binary\r\n\r\n"; post_data.push(new Buffer(part, 'utf8')); post_data.push(new Buffer(file_contents, 'binary')); post_data.push(new Buffer("\r\n--" + boundary + "--\r\n\r\n", 'utf8')); var post_length = 0; for(var i = 0; i &lt; post_data.length; i++) { post_length += post_data[i].length; } var options = { host: 'uploads.gdata.youtube.com', port: 443, path: '/feeds/api/users/default/uploads', method: 'POST', headers: { 'Authorization': 'Bearer ' + access_token, 'X-GData-Key': myDeveloperKey, 'Slug': 'video.mp4', 'Content-Type': 'multipart/related; boundary="' + boundary + '"', 'Content-Length': post_length, 'Connection': 'close' } } var req = https.request(options, function(res) { res.setEncoding('utf8'); console.dir(res.statusCode); console.dir(res.headers); var response = ''; res.on('data', function(chunk) { response += chunk; }); res.on('end', function() { console.log( "We got response: " ); console.log(response); }); }); for (var i = 0; i &lt; post_data.length; i++) { req.write(post_data[i]); } req.on('error', function(e) { console.error(e); }); req.end(); }); }); }); p_req.write(p_data); p_req.end(); </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload