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

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了使用npm twit将图像上传到Twitter时“媒体类型无法识别”相关的知识,希望对你有一定的参考价值。

我正在开发一个快速节点应用程序,当用户将图像输入到表单时,该应用程序会发布到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('/')
})

谢谢!

答案

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

以上是关于使用npm twit将图像上传到Twitter时“媒体类型无法识别”的主要内容,如果未能解决你的问题,请参考以下文章