node.js 错误 : body attribute missing in multipart when uploading to youtube

标签 node.js upload youtube

尝试上传视频时,我收到以下错误:

2014-04-08T20:22:39.570207+00:00 app[web.1]: Error: Body attribute missing in multipart.
2014-04-08T20:22:39.570402+00:00 app[web.1]:     at OAuth2Client.request (/app/node_modules/googleapis/lib/auth/oauth2client.js:224:20)
2014-04-08T20:22:39.570207+00:00 app[web.1]:     at Error (<anonymous>)
2014-04-08T20:22:39.570207+00:00 app[web.1]:     at /app/node_modules/request/request.js:1105:28
2014-04-08T20:22:39.570207+00:00 app[web.1]:     at Array.forEach (native)
2014-04-08T20:22:39.570207+00:00 app[web.1]:     at Request.multipart (/app/node_modules/request/request.js:1103:13)
2014-04-08T20:22:39.570207+00:00 app[web.1]:     at Request.self._buildRequest (/app/node_modules/request/request.js:306:12)
2014-04-08T20:22:39.570207+00:00 app[web.1]:     at Request.init (/app/node_modules/request/request.js:503:10)
2014-04-08T20:22:39.570207+00:00 app[web.1]:     at request (/app/node_modules/request/index.js:50:11)
2014-04-08T20:22:39.570207+00:00 app[web.1]:     at new Request (/app/node_modules/request/request.js:97:8)

代码:
var Video = require('./video.js'); 

var googleapis = require('googleapis');

app.post("/upload", function (req, res) { 

//get the file name
console.log("## /upload called for file: " + JSON.stringify(req.files, undefined, 2)
    + "\n## Title: " + req.body.title
    + "\n## Description: " + req.body.description);
var filename = req.files.file.name;
var extensionAllowed = [".MOV", ".MP4", ".AVI", ".WMV"];
var maxSizeOfFile = 100000;
var msg = "";
var i = filename.lastIndexOf('.');

// get the temporary location of the file
var tmp_path = req.files.file.path;

// set where the file should actually exists - in this case it is in the "images" directory
var target_path = __dirname + '/upload/' + req.files.file.name;

var file_extension = (i < 0) ? '' : filename.substr(i);

if ((file_extension in oc(extensionAllowed)) && ((req.files.file.size / 1024) < maxSizeOfFile)) {

    // handle upload to youtube
    googleapis.discover('youtube', 'v3').execute(function (err, client) {
    var metadata = {
        snippet: {
            title: req.body.title,
            description: req.body.description
        },
        status: {
            privacyStatus: 'private'
        }
    };

    // pass auth, refresh tokens
    oauth2Client.credentials = {
      access_token: access_token,
      refresh_token: refresh_token
    }

    client.youtube.videos.insert({
        part: 'snippet, status'
    }, metadata)
        .withMedia('video/MOV', fs.readFileSync(tmp_path))
        .withAuthClient(oauth2Client).execute(function (err, result) {
            if (err) console.log(err);
            else console.log(JSON.stringify(result, null, ' '));

            // save uploaded video to db
             var video = new Video({
               id: result.id,
               title: result.snippet.title,
               description: result.snippet.description,
               publishedAt: result.snippet.publishedAt,

             });
             video.save(function(err) {
               if(err) {
                 console.log(err);
               } else {
                 console.log("saved new video: ", JSON.stringify(video, null, "\t"));
                 // done(null, video);
               };
             });

            // save upload video to its owner
            User.findOne({ oauthID: req.user.oauthID}, function(err, user) {
             if(err) { console.log(err); }
             if (!err && user != null) {

               user.videos.push(video);

               user.save(function(err) {
                 if(err) {
                   console.log(err);
                 } else {
                   console.log("saving uploaded video to user...");
                 };
             });
             };
            });
        });
    });
    msg = "File " + JSON.stringify(req.files.file.name) + " successfully uploaded to youtube!"
} else {
    // delete the temporary file, so that the explicitly set temporary upload dir does not get filled with unwanted files
    fs.unlink(tmp_path, function (err) {
        if (err) throw err;
    });
    msg = "File upload failed. File extension not allowed and size must be less than " + maxSizeOfFile;
}
res.end(msg);
});

任何帮助将非常感激。

最佳答案

我遇到了同样的错误,很可能是谷歌代码中的一个错误。

据我通过调试源代码所见,oauth 模块正在尝试刷新 token ,因此它发出了 2 个请求,而第二个请求因上述错误而失败。

有一个issue opened in github对于这个确切的问题。

关于node.js 错误 : body attribute missing in multipart when uploading to youtube,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22947688/

相关文章:

javascript - Expressjs/node.js 相当于 php 中的 exit()

利用下载速度提高上传速度的算法

javascript - 如何检索我的 YouTube channel 播放列表?

javascript - If else语句,用于更改CSS中的颜色

javascript - 断开 sails.js socket.io 的逻辑

javascript - 为什么删除 global 和删除 var 在 Node 中会导致不同的输出?

php - 检查文件上传扩展(PHP/Jquery/Javascript)

javascript - 如何触发 Uploadify onError 事件处理程序?

javascript - 在WordPress的Youtube Player中包含跳至特定时间的链接

sql-server - 在sql查询中传递node.js参数