使用node.js将video上传到YouTube

我已经与官方的Google API节点客户端取得了一些进展,但我已经到了一个死路一条,弄清楚如何将我的videofile upload到YouTube。

我有一个video: example.mp4

我有这个代码:

 OAuth2Client = googleapis.OAuth2Client; var oauth2Client = new OAuth2Client('XXXXXX', 'XXXXXXX', 'http://callback_url'); googleapis.discover('youtube', 'v3').execute(function(err, client) { if (err) console.log(err); // I think the following is the bones of what I want to do client.youtube.videos.insert({}).execute(function(err, client) { if (err) console.log(err); }); }); 

我只使用插入方法(我期望没有传递参数)的错误,客户端初始化并返回罚款。

我不知道如何将video(与脚本相同的目录)传递给YouTube。 只是一个指针将不胜感激。

这个代码如何:

 var googleapis = require('googleapis'); googleapis.discover('youtube', 'v3').execute(function(err, client) { var metadata = { snippet: { title:'title', description: 'description'}, status: { privacyStatus: 'private' } }; client .youtube.videos.insert({ part: 'snippet,status'}, metadata) .withMedia('video/mp4', fs.readFileSync('user.flv')) .withAuthClient(auth) .execute(function(err, result) { if (err) console.log(err); else console.log(JSON.stringify(result, null, ' ')); }); }); 

你可以使用这个包来做到这一点: https : //github.com/h2non/youtube-video-api

这段代码适用于我:

 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 = '<?xml version="1.0"?>' + '<entry xmlns="http://www.w3.org/2005/Atom" xmlns:media="http://search.yahoo.com/mrss/" xmlns:yt="http://gdata.youtube.com/schemas/2007">' + ' <media:group>' + ' <media:title type="plain">' + title + '</media:title>' + ' <media:description type="plain">' + description + '</media:description>' + ' <media:category scheme="http://gdata.youtube.com/schemas/2007/categories.cat">' + category + '</media:category>' + ' <media:keywords>' + keywords + '</media:keywords>' + ' </media:group>' + '</entry>'; 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, 'ascii')); post_data.push(new Buffer(file_contents, 'binary')); post_data.push(new Buffer("\r\n--" + boundary + "--"), 'ascii'); var post_length = 0; for(var i = 0; i < post_data.length; i++) { post_length += post_data[i].length; } var options = { host: 'uploads.gdata.youtube.com', port: 80, path: '/feeds/api/users/default/uploads?alt=json', method: 'POST', headers: { 'Authorization': 'GoogleLogin auth=' + auth_key, 'GData-Version': '2', 'X-GData-Key': 'key=' + exports.developer_key, 'Slug': 'example.mp4', 'Content-Type': 'multipart/related; boundary="' + boundary + '"', 'Content-Length': post_length, 'Connection': 'close' } } var req = http.request(options, function(res) { res.setEncoding('utf8'); var response = ''; res.on('data', function(chunk) { response += chunk; }); res.on('end', function() { console.log(response); response = JSON.parse(response); callback(response); }); }); for (var i = 0; i < post_data.length; i++) { req.write(post_data[i]); } req.on('error', function(e) { console.error(e); }); req.end(); });