“无法识别的媒体类型"使用npm twit将图像上传到Twitter时 [英] "media type unrecognized" when uploading an image to Twitter using npm twit

查看:50
本文介绍了“无法识别的媒体类型"使用npm twit将图像上传到Twitter时的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发一个快速节点应用程序,当用户将图像输入表单时,该应用程序会发布到Twitter.我在上传之前将图像保存在本地,这是可行的.在将文件编码为base64之后,我尝试使用twit的媒体/上传功能将base64编码的文件上传到Twitter.当我这样做时,出现错误消息无法识别的媒体类型."

I am working on an express node app that posts to twitter when a user inputs an image into a form. I am saving the image locally before uploading, which works. After I encode the file to base64, I try to upload the base64-encoded file to Twitter using twit's media/upload feature. When I do this, I get an error saying "media type unrecognized."

这是我的代码:

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('/')
})

谢谢!

推荐答案

使用 postMediaChunked 函数

var filePath = '/absolute/path/to/file.png'
T.postMediaChunked({ file_path: filePath }, function (err, data, response) {
  console.log(data)
})

这篇关于“无法识别的媒体类型"使用npm twit将图像上传到Twitter时的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆