javascript - 将图像从直接消息媒体附加到 Twitter 状态
问题描述
大家好,我尝试使用 Twitter https://api.twitter.com/1.1/statuses/update.json
POST 请求 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.id
and作为这样的参数值,但仍然出现错误attachment.media.id_str
media_ids
Invalid 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);
};
})
谢谢
解决方案
您不能通过相同的直接消息重新发布媒体media_id_string
,因为它已被“使用”。您也不能发布 URL 并显示图像,因为它对发送者和接收者是私有的。
您将需要实施一个三个阶段的过程:
- 将直接消息中的图像下载到本地文件。您将需要使用 OAuth 用户身份验证来执行此操作。
- 在 Twitter 上将媒体作为新实体上传,并抓取新的
media_id_string
- 发布一条新的推文,
media_id_string
并附上。
请注意,您应该考虑重新发布通过直接消息发送的图像对隐私的影响,并确保发件人知道它们可能会被重新发布为可公开访问。
推荐阅读
- python - 如何使用 QSqlTableModel 为 Qtableview 创建过滤器行。?
- python - /usr/local/bin/pip:错误的解释器:/usr/local/opt/python/bin/python3.7
- python - 如何修复 Discord.py 中的未知消息 (404)
- objective-c - 内部可编辑的 NSTextField 不是 .titled NSPanel
- java - 没有 Spring Boot 的 Java Spring 测试 - 错误 java.lang.IllegalStateException: Failed to load ApplicationContext
- java - 将数组连接到来自firestore的字符串
- javascript - 相同的功能在一个页面中抛出“No 'Access-Control-Allow-Origin'”但在其他页面中没有
- javascript - 如何在默认按钮组子元素上设置焦点(页面加载后)
- javascript - 如何在搜索前隐藏答案
- c# - 如何在 Visual Studio 的存储过程中使用查询来使用另一列中的值从表中获取 ID