EDIT: My mistake - this seems alright. I wasn’t waiting long enough after FINALIZE to check the status.
Thanks for sharing that example!
I had wrestled with the ‘media_category’ parameter for a bit and saw in your example that you used 'media_category': 'tweetvideo' instead of 'media_category': 'tweet_video'. I’ve seen both mentioned - which should be used?
After reviewing your example, I believe my code is aligned with the example, as I get the correct response for the 3 phases. That being said, calling status before and after the ‘check_after’ time I get a 400 error, as mentioned in the title.
Here’s my function:
async function postMediaChunked(twitterClient, file) {
return await new Promise(async (resolve, reject) => {
const { err, data } = await twitterClient.post('media/upload', {
command: 'INIT',
media_type: file.mime,
media_category: parseMediaCategory(file.mime),
total_bytes: fs.statSync(file.path).size
})
if (err) reject(err)
var mediaIdStr = data.media_id_string
let isStreamingFile = true
let isUploading = false
let segmentIndex = 0
const fStream = fs.createReadStream(file.path, { highWaterMark: 1 * 1024 * 1024 })
fStream.on('data', async function (buff) {
fStream.pause()
isUploading = true
const { data } = await twitterClient.post('media/upload', {
command: 'APPEND',
media_id: mediaIdStr,
segment_index: segmentIndex,
media: buff.toString('base64')
})
isUploading = false
if (!isStreamingFile && !isUploading) {
_finalizeMedia(mediaIdStr)
}
fStream.resume()
segmentIndex++
})
fStream.on('end', function () {
isStreamingFile = false
if (!isUploading && !isStreamingFile) {
_finalizeMedia(mediaIdStr)
}
})
const _finalizeMedia = async function (mediaIdStr) {
const { data } = await twitterClient.post('media/upload', {
'command': 'FINALIZE',
'media_id': mediaIdStr
})
console.log('FINALIZE:', data)
console.log('============')
resolve(data)
}
})
}