Why it returns “error: Not found” when I use the “APPEND” command?
var request = require('request');
var fs = require('fs');
var async = require('async');
// Twitter Access Tokens
var oauth =
{
consumer_key: '',
consumer_secret: '',
token: '',
token_secret: ''
};
var TwitterVideoUpload = function (oauth) {
this.oauth = oauth;
this.CHUNK_SIZE = 100*1024; // 100 KB
};
TwitterVideoUpload.prototype.setChunkSize = function (chunk_size){
this.CHUNK_SIZE = chunk_size;
}
TwitterVideoUpload.prototype.uploadFile = function (fileInfo, callbackResponse) {
if (Object.keys(fileInfo).length == 0) {
fileInfo = {
path: null,
total_bytes: 0
};
console.error('Complete the file info: '+ JSON.stringify(fileInfo));
}
var media = null,
oauth = this.oauth,
CHUNK_SIZE = this.CHUNK_SIZE,
buffer = new Buffer(CHUNK_SIZE),
data = [];
// synchronous requests: INIT, APPEND, FINALIZE
async.series([
function (callback) {
// "INIT" command to tnitialize the upload
request.post({
url: "https://upload.twitter.com/1.1/media/upload.json", oauth: oauth, host: "upload.twitter.com", protocol: "https:", formData: {
command: "INIT",
media_type: 'video/mp4',
total_bytes: fileInfo.total_bytes
}
}, function requestCallback(err, res, body) {
media = JSON.parse(body);
callback();
});
},
function (callback) {
// open file, read only
var file = fs.openSync(fileInfo.path, 'r'),
nread;
// push synchronous chunks of CHUNK_SIZE in data array
while ( (nread = fs.readSync(file, buffer, 0, CHUNK_SIZE, null)) !== 0) {
data.push((nread < CHUNK_SIZE ? buffer.slice(0, nread) : buffer).toString('base64'));
}
// close the file
fs.close(file, function (err) {
if (err) throw err;
callback();
});
}, function (callback) {
// set request headers
var headers = {
'Content-Type': 'video/mp4',
'Content-Transfer-Encoding': 'base64'
};
var i = 0,
options = {
url: 'https://upload.twitter.com/1.1/media/upload.json',
method: 'POST',
oauth: oauth,
headers: headers,
form: null
},
form = null;
// synchronous iterate over data and send it to the twitter by using the "APPEND" command
async.eachSeries(data, function (base64, callbackData) {
options.form = {
command: "APPEND",
media_id: media.media_id,
media: data[i],
segment_index: i++<img src="/uploads/default/original/2X/2/2393b28a9dcc5d35c4416abbe4f545995b48b945.png" width="690" height="299">
};
request(options, function (error, response, body) {
if (!error) {
callbackData();
}
});
}, function done() {
// send FINALIZE command when all data has been sent
request({
url: 'https://upload.twitter.com/1.1/media/upload.json',
method: 'POST',
oauth: oauth,
headers: headers,
form: {
command: "FINALIZE",
media_id: media.media_id
}
}, function (error, response, body) {
if (!error) {
callback();
}
});
});
},
function (callback){
callbackResponse(media.media_id);
}
]);
};
var twitterUpload = new TwitterVideoUpload(oauth);
twitterUpload.uploadFile({
path: './video.mp4',
total_bytes: 752333
}, function (media_id) {
console.log('Media ID', media_id);
});
The video that I’m using is this: https://video.twimg.com/ext_tw_video/601491637433475073/pu/vid/640x360/DRQCQaiww7XlAYJP.mp4
These are the headers that I’m sending when I use the command “APPEND” (segment_index was 0, but I skipped one step in the debugger and becomes 2):
Click here to see the request headers
Thanks !