使用Twit发布带有机器人的视频


function fridayNight(){
const videoPath = "C:\GitHub\DivasLive\DivasLive\nsync.mp4";
console.log("It's Friday night and I just Got Paid!");
var b64content = fs.readFileSync(videoPath, { encoding: 'base64' });
var mediaType = MIM.getMIMEType(videoPath);
T.post('media/upload', { media_data: b64content, media_type: mediaType }, function (err, data, 
response) 
{
if(err) 
{
console.log(err);
} else 
{ 
console.log(data);
var mediaIdStr = data.media_id_string
var params = { status: "Just got paid!", media_id: [mediaIdStr] };
T.post('statuses/update', params, function (err, data, response) 
{
console.log(data);
console.log(err);
});
};
});
};

我一直得到一个无法识别的400:Media类型,但我试图在第88行中明确定义它。以下也是全部要点。https://gist.github.com/MetzinAround/25b5771564aa7de183391398db52dbef

对于视频和GIF,您需要使用分块媒体上传方法-您不能在"单次发射";,根据Twitter文档,它有多个阶段(INIT、APPEND、FINALIZE(。

请注意,要上传视频或gif(tweet_video、amplify_video和tweet.gif(,您需要使用分块上传端点。

事实证明,twit模块有一个助手方法postMediaChunked来完成这项工作,这也节省了您必须告诉Twitter数据的mime类型,这意味着您可以删除mim模块的导入。

这里有一个只做媒体部分的最小示例-您只需要提取media_id_string并在statuses/update调用中使用它:

// Create an Twitter object to connect to Twitter API
const Twit = require('twit')
// Making a Twit object for connection to the API
const T = new Twit(config)
var filePath = '/Users/myuser/Downloads/robot.mp4'
T.postMediaChunked({
file_path: filePath
}, function (err, data, response) {
console.log(data)
})

输出:

{
media_id: 1379414276864151600,
media_id_string: '1379414276864151557',
media_key: '7_1379414276864151557',
size: 924669,
expires_after_secs: 86400,
processing_info: { state: 'pending', check_after_secs: 1 }
}

(注意,在JavaScript中,您应该始终使用Twitter ID的字符串版本——正如您在这里看到的,media_id中的数字版本与media_id_string不匹配,因为JavaScript无法正确处理长整数,并且损坏了数值(

最新更新