node.js错误:上传到youtube时,multipart中缺少body属性

时间:2014-04-08 20:39:02

标签: 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);
});

非常感谢任何帮助。

1 个答案:

答案 0 :(得分:0)

我收到同样的错误,很可能是Google代码中的错误。

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

这个问题确实存在issue opened in github