使用npm twit将图像上传到Twitter时“媒体类型无法识别”

问题描述 投票:0回答:1

我正在开发一个快速节点应用程序,当用户将图像输入到表单时,该应用程序会发布到twitter。我在上传之前在本地保存图像,这有效。在我将文件编码为base64之后,我尝试使用twit的媒体/上传功能将base64编码的文件上传到Twitter。当我这样做时,我收到一个错误,说“媒体类型无法识别”。

这是我的代码:

app.post('/tweet', function(req, res){
   var time = new Date().getTime()
   let image = req.files.image
   var imgpath = './images/img' + time + '.jpg'

   image.mv(imgpath, function(err) {
       if (err){
         return res.status(500).send(err);
       }
   });
   var b64content = fs.readFileSync(imgpath, { encoding: 'base64' })

       T.post('media/upload', {media: b64content}, function(err, data, res) {
           if (err) console.log(err);
           console.log(data);
           T.post('statuses/update', {status: 'posted picture at: ' + time, media_ids: [data.media_id_string]}, function(err, params, res) {
           if (err) console.log(err);
            console.log(params);
        });
    });


   return res.redirect('/')
})

谢谢!

node.js express twitter
1个回答
0
投票

得到它了!。我需要将T.post代码放在image.mv函数的括号中

© www.soinside.com 2019 - 2024. All rights reserved.