postMediaChunked Fails to Check processing_info.state == 'pending'
(Updated) When calling postMediaChunked(....) then post('media/metadata/create' ...) then post('statuses/update'...) as suggested, Twitter API can report 'Not valid video' after this last call.
On investigation, the explanation below of the 'Not valid video' error wasn't totally correct.
The simplest solution is to add a delay of N seconds after postMediaChunked(...). A hint of the delay is returned by 'postMediaChunked(...)' in 'data.processing_info.check_after_secs'
Delay code... const result = await new Promise(resolve => setTimeout(resolve, data.processing_info.check_after_secs/1000));
A better solution would be to use... this.T.get('media/upload', {command: 'STATUS', media_id: mediaIdStr}, function (err, data, resp){ ...} while uploading the file to test its completeness.
Hope that helps
It appears that because Twitter takes time to process the incoming file, calling the 'status/update' too soon creates the error.
However, postMediaChunked(...) (via Twitter API upload/media command= 'FINALIZE') does return a processing_info.state == 'pending'.
Even though Twitter API docs recommend checking this value and waiting for processing_info.state == 'success', twit doesn't bother.
Can someone suggest a workaround?
processing_info is returned in FINALIZE with state set to pending when media not fully processed yet. Posting a tweet with the media in this state will fail with Not valid video error if you uploaded video.
To handle pending state, wait for check_after_secs seconds then check media upload status using GET media/upload with command set to STATUS. Keep doing this until state is either succeeded or failed. State will transition from pending to in_progress to either succeeded or failed.
UPDATE: Found another problem. twit.get('media/upload', { command: 'STATUS', media_id: 123456 }) gets mishandled and parameters are not sent as query parameter but passed as body due to internal path dependent hacks that sees path media/upload and thinks it's one of media upload commands that use HTTP POST.
yep, same issue, I guess we need to use some other http client manual to check the status
I ended up rewriting the code but I think donpark's solution could work better than mine.
I found that check_after_secs almost always returned 1 second but it was never enough time anyway and several STATUS checks were needed. But by addding an addtional 1 second delay before checking for succeeded gave Twitter time to process the upload. Backing off like this reduces the API call overhead without producing a significant lack of responsiveness.
I found that Twitter sometimes appears to return state == success but fails when attempting to use the returned media_idin a post. Maybe Twitter doesn't always finish processing the media! In this case a solution is to retry uploading the whole media again until it works.
well thats a shitty solution, it sucks there is no proper handling of this api method, its very hard to upload these damn videos!
Looks like this basic 4yrs-old library https://github.com/desmondmorris/node-twitter handles the API correctly.
For chunked media upload example code using the lib, see: https://github.com/mikan3rd/twitter-manager/blob/master/functions/src/TwitterClient.ts
As far as slop seconds to add (example used 5 second), I had no problem with just 1 second so far but I suspect the timing gap is load dependent so add a reasonable retry logic to cover your need without hammering Twitter API which can't differentiate bugs from spamming.