【问题标题】:uploading base64 encoded image to twiiter将 base64 编码图像上传到 Twitter
【发布时间】:2016-04-13 05:01:01
【问题描述】:

我正在使用 nodejs 的 Twitter 模块将媒体上传到我的 Twitter 帐户。作为示例程序来自这里https://github.com/desmondmorris/node-twitter/tree/master/examples#media

我没有使用来自系统的图像,而是传递图像的 base64 编码字符串。当我运行这个程序时,它会在控制台中循环输出base64 string(image),过了一会儿它会崩溃并且不会上传我的图像。

// Load your image
var data = require('fs').readFileSync(image);

// Make post request on media endpoint. Pass file data as media parameter
client.post('media/upload', {media: image}, function(error, media, response){

  if (!error) {

    // If successful, a media object will be returned.
    console.log(media);

    // Lets tweet it
    var status = {
      status: 'I am a tweet',
      media_ids: media.media_id_string // Pass the media id string
    }

    client.post('statuses/update', status, function(error, tweet, response){
      if (!error) {
        console.log(tweet);
      }
    });

  }
  else{
    console.log("error upload");
  }
});

所以我尝试了不同的方法。我将base64编码图像保存到out.png

fs.writeFile("out.png", data.image, 'base64', function (err) {
                console.log(err);                
            });

var data = require('fs').readFileSync("out.png");

这个代码输出错误说

错误:ENOENT:没有这样的文件或目录,打开 'D:\Local server\nodejs\out.png'

此文件确实存在,我已确认

更新

Api 返回[Error: Status Code: 400]

【问题讨论】:

  • 您的函数中的data.image 是什么?它应该是上传图片的路径。
  • 它是base64编码的字符串
  • 什么不起作用?
  • 它在控制台中循环打印base64字符串并崩溃
  • 更新了更多细节问题

标签: node.js twitter


【解决方案1】:

您好,有几个 nodejs api 允许用作 Twitter 的客户端库,您可以使用它们将您的媒体上传到您的 Twitter 帐户,最流行的 twitterare node-twitter-api

【讨论】:

  • @AkashKumar 你能提供更多细节吗,你的问题是客户端还是服务器端
  • 更新了更多细节问题
猜你喜欢
  • 2022-01-11
  • 1970-01-01
  • 2015-09-05
  • 1970-01-01
  • 2015-06-17
  • 2011-11-22
  • 2016-10-23
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多