【问题标题】:"media type unrecognized" when uploading an image to Twitter using npm twit使用 npm twit 将图像上传到 Twitter 时出现“无法识别的媒体类型”
【发布时间】:2018-08-07 17:50:40
【问题描述】:

我正在开发一个 express 节点应用程序,当用户将图像输入表单时,它会发布到 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】:

    知道了!我需要将T.post 代码放在image.mv 函数的括号中

    【讨论】:

      【解决方案2】:

      使用postMediaChunked函数

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

      【讨论】:

      • 口头解释对您的回答很有帮助
      猜你喜欢
      • 2018-10-20
      • 2015-10-10
      • 1970-01-01
      • 2021-10-16
      • 1970-01-01
      • 2021-11-03
      • 2019-03-15
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多