首页 > 解决方案 > 将图像从直接消息媒体附加到 Twitter 状态

问题描述

大家好,我尝试使用 Twitter https://api.twitter.com/1.1/statuses/update.jsonPOST 请求 API 发布状态。我也想用附加的图像更新状态,但是图像已经在直接消息中,所以我通过使用 GET 请求获得了直接消息,并且我得到了这样的消息对象..

{
  type: 'message_create',
  id: 'xxxxxxxxxxxxx',
  created_timestamp: 'xxxxxxxxxxxxx',
  message_create: {
    target: { recipient_id: 'xxxxxxxxxxxxxxxx' },
    sender_id: 'xxxxxxxxxxxxxx',
    message_data: {
      text: 'This is trigger message with image,oy! https://x.xx/xxxxxxxxx',
      entities: [Object],
      attachment: {
        type: 'media',
        media: {
          id: xxxxxxxxxxxxxxx,
          id_str: 'xxxxxxxxxxxxxxx',
          indices: [ 39, 62 ],
          media_url: 'https://ton.twitter.com/1.1/ton/data/dm/xxxxx/xxxxx/gr_SQawQ.jpg',
          media_url_https: 'https://ton.twitter.com/1.1/ton/data/dm/xxxxx/xxxxx/gr_SQawQ.jpg',
          url: 'https://x.xx/xxxxx',
          display_url: 'pic.twitter.com/xxxxx',
          expanded_url: 'https://twitter.com/messages/media/xxxxxx',
          type: 'photo',
          sizes: {
            medium: [Object],
            thumb: [Object],
            large: [Object],
            small: [Object]
          }
        }
      }
    }
  }
}

我的问题是,您如何发布带有来自直接消息的媒体图像的附加图像的状态?

我尝试使用该响应中的attachment.media.idand作为这样的参数值,但仍然出现错误attachment.media.id_strmedia_idsInvalid media.

const text = message.message_create.message_data.text;
const attachment = message.message_create.message_data.attachment;

const payload = {
    status: text
};
attachment && (payload.media_ids = [attachment.media.id_str]); //i've tried using id_str and id

T.post('statuses/update', payload, (error, data, response) => {
    if (!error) {
        resolve({
            message: `successfuly posting new status with DM id: ${message.id}`,
            data
        });
    } else {
        reject(error);
    };
})

谢谢

标签: javascriptapitwittertwitter-oauth

解决方案


您不能通过相同的直接消息重新发布媒体media_id_string,因为它已被“使用”。您也不能发布 URL 并显示图像,因为它对发送者和接收者是私有的。

您将需要实施一个三个阶段的过程:

请注意,您应该考虑重新发布通过直接消息发送的图像对隐私的影响,并确保发件人知道它们可能会被重新发布为可公开访问。


推荐阅读