【问题标题】:How can I upload multiple images to twitter?如何将多张图片上传到 Twitter?
【发布时间】:2021-07-02 22:21:39
【问题描述】:

我对 nodejs 和 javascript 真的很陌生。我正在尝试创建一个 twitter 机器人来发布带有两张图片的推文。但是,我遇到的所有实现都使用单一媒体上传,它只对单个图像文件有帮助。

const Twitter = require("twitter")
const fs = require("fs")

const client = new Twitter({
  consumer_key: process.env.CONSUMER_KEY,
  consumer_secret: process.env.CONSUMER_SECRET,
  access_token_key: process.env.ACCESS_TOKEN_KEY,
  access_token_secret: process.env.ACCESS_TOKEN_SECRET
})

const imageData1 = fs.readFileSync("./quote.jpg"); 
const imageData2 = fs.readFileSync("./meaning.jpg"); // the second image i want to upload

client.post("media/upload", {media: imageData1}, function(error, media, response) {
  if (error) {
    console.log(error)
  } else {
    const status = {
      status: "I tweeted from Node.js!",
      media_ids: media.media_id_string
    }
    console.log(media)

    client.post("statuses/update", status, function(error, tweet, response) {
      if (error) {
        console.log(error)
      } else {
        console.log("Successfully tweeted an image!")
      }
    })
  }
})

感谢任何有关不同方法或我做错任何事情的帮助。 干杯!

【问题讨论】:

    标签: javascript node.js api twitter


    【解决方案1】:

    我可以通过 Twit library 做到这一点

    您需要单独上传每张图片,然后将此新媒体的 id 添加到数组中。当所有图片上传完毕后,调用带有媒体ID数组的statuses/update端点,图片将附加到推文中

    const Twit = require('twit')
    const fs = require('fs');
    
    const client = new Twit({
        consumer_key: '<CONSUMER_KEY>',
        consumer_secret: '<CONSUMER_SECRET>',
        access_token: '<ACCESS_TOKEN>',
        access_token_secret: '<ACCESS_TOKEN_SECRET>'
    });
    
    const files = ['image1.png', 'image2.png'];
    const status = 'This tweet has multiple images';
    tweetImages(files, status);
    
    function tweetImages(files, status) {
      let mediaIds = new Array();
      files.forEach(function(file, index) { 
        uploadMedia(file, function(mediaId) {
          mediaIds.push(mediaId);
          if (mediaIds.length === files.length) {
            updateStatus(mediaIds, status);
          }
        });
      });
    };
    
    function uploadMedia(file, callback) {
      client.post('media/upload', { media: fs.readFileSync(file).toString("base64") }, function (err, data, response) {
        if (!err) {
          let mediaId = data.media_id_string;
          callback(mediaId);
        } else {
          console.log(`Error occured uploading content\t${err}`);
          process.exit(-1);
        }
      });
    }
    
    function updateStatus(mediaIds, status) {
      let meta_params = {media_id: mediaIds[0]};
      client.post('media/metadata/create', meta_params, function (err, data, response) {
        if (!err) {
          let params = { status: status, media_ids: mediaIds};
          client.post('statuses/update', params, function (err, data, response) {
            if (err) {
              console.log(`Error occured updating status\t${err}`);
            }
          });
        } else {
          console.log(`Error creating metadata\t${err}`);
          process.exit(-1);
        }
      });
    }
    

    【讨论】:

    • 谢谢约瑟夫!这真的很有帮助。我当时使用 Python 版本来完成这个过程,就像在媒体文件列表中输入一个额外的条目一样简单。
    猜你喜欢
    • 2020-09-20
    • 2020-06-24
    • 2020-07-05
    • 2015-09-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-04-16
    • 1970-01-01
    相关资源
    最近更新 更多