0
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);
  });
   };
 });
};

I keep getting a 400: Media type unrecognized, but I'm trying to explicitly define it in line 88. Here's the full gist as well. https://gist.github.com/MetzinAround/25b5771564aa7de183391398db52dbef

Pj Metz
  • 1
  • 2

1 Answers1

0

For video and GIFs, you need to use the chunked media upload method - you cannot do it in a "single shot", it has multiple stages (INIT, APPEND, FINALIZE), per the Twitter docs.

Please note that to upload videos or GIFs (tweet_video, amplify_video, and tweet_gif), you need to use the chunked upload end-point.

It turns out that the twit module has a helper method for doing this, postMediaChunked - this also saves you having to tell Twitter the mime type of the data, which means you can remove the import of the mim module.

Here's a minimal example of just doing the media part - you just need to extract the media_id_string and use that in the statuses/update call:

// 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)
})

output:

{
  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 }
}

(note that, in JavaScript, you should always use the string versions of Twitter IDs - as you can see here, the numeric version in media_id does not match media_id_string, as JavaScript cannot properly handle long integers, and has mangled the numeric value)

Andy Piper
  • 11,422
  • 2
  • 26
  • 49
  • Am I understanding that Data is an object and media_id_string is a property of that object? IF I need to call Media_id_string later, do I do that with data.media_id_string? – Pj Metz Apr 08 '21 at 21:10
  • yes, that should do it. Data is a JSON response from the API. – Andy Piper Apr 09 '21 at 14:54