No currently available Javascript APIs

oauth
javascript

#1

Hello there

I’ve been trying to upload large media files to my account and have had several problems so far. I first tried to use a small python script and it worked, a video was uploaded to my account. So, once assured it worked in some language, I tried then to implement the same in javascript which was my initial goal. I then went over to the Twitter libraries page only to find 2 libraries under Javascript:

  • one of them hasn’t had a new commit in over two years and has it’s build failing according to README shields
  • the other has more than 3 years of inactivity and apparently just works with streams

I tried using only requests but I don’t fully get how complete the authentication flow. The documentation is a little too… cryptic… And there are no other examples with a sequence of valid requests. I would be very thankful if someone could give me a hand in this javascript scenario; it seems to be the only neglected language in twitter’s library.

All of these requests should be language-agnostic since they’re just requests. I just want to understand how I can use my 4 keys to upload a video language-agnostically using a tool, such as, but not limited to, curl.

Thanks for reading this


#2

twitter and twit are both pretty popular, in my experience, they just are not currently listed on the third-party libraries page. The twitter module also has a worked chunked media upload example that may help you.


#3

Hi Andy, thanks for replying
I tried using precisely this example, which returns me 503 code almost every time I try uploading, with minimal (only the required) changes to the code. I can post it if you want.

First I tried using twit but failed and didn’t find an example so I just thought I was doing something wrong. Then I moved on to the twitter package: I get to the media Id string part, I get it on stdout but then when it’s about to do it’s first APPEND, I get a 503 error code. When I don’t get a 503 code, as in, I change which video I’m uploading, I get error code 400, which is Bad Request, which would imply the library is unreliable. It’s worth noting that all videos I’m trying to upload are of 14MB or less.


#4

My usual starting point would be to suggest that you try our Python sample, but you’ve already done that… so we do indeed know that works, and it seems like there’s something strange going on in the JS cases. Are you able to provide a link to one of the videos you’ve been trying to upload, and ideally your code, so that one of us can have a look and try to reproduce what you’re seeing? There’s also an example for this related to the twit library worth having a play with.

My initial suspicion was that this might have been related to the async nature of JS, but in the case of the example in the twitter repo, it looks like it is using Promises to control the flow in the way I’d expect.


#5

Hi again
Sorry for the late response
This is what I had for twitter package:

const Twitter = require('twitter');
var fs = require('fs')
var client = new Twitter({
    consumer_key: '...',
    consumer_secret: '...',
    access_token_key: '...',
    access_token_secret: '...'
});

const pathToMovie = '/mnt/4ADE1465DE144C17/Videos/video.mp4';
const mediaType   = 'video/mp4'; // `'video/mp4'` is also supported
const mediaData   = require('fs').readFileSync(pathToMovie);
const mediaSize   = require('fs').statSync(pathToMovie).size;

let mediaIdString = '';
let mediaId = 0;

//  reading file (GIF)
const gifFile = fs.readFileSync(pathToMovie);

//  getting file details
const gifFileStat = fs.statSync(pathToMovie);
const gifSize = gifFileStat.size;
const contentType = 'video/mp4';

console.log(`vid size in bytes ==> ${gifSize}`)

//  calling uploadMedia
uploadMedia().then((result) => {
    console.log('UPLOAD SUCCESSFUL');
}).catch((err) => {
    console.log(err);
});

function uploadMedia () {
    //    STEP 1 (INIT)
    return client.post('media/upload', {
        command: 'INIT',
        total_bytes: gifSize,
        media_type: contentType
    }).then((response) => {
        console.log(`INIT \t response ==> ${JSON.stringify(response)}`)

        mediaIdString = response.media_id_string;
        mediaId = response.media_id;

        console.log(`mediaId ==> ${mediaId}`);

        console.log(`media id string ==> ${mediaIdString}`);

        //  step 2 (APPEND)
        return client.post('media/upload', {
            command: 'APPEND',
            media_id: mediaIdString,
            media_data: gifFile,
            segment_index: 0
        });
    }).then((response) => {
        console.log(`APPEND \t response ==> ${JSON.stringify(response)}`);

        //  STEP 3 (STATUS)
        var status = undefined;
        while(status == undefined){
            status = client.get('media/upload', {
                command: 'STATUS',
                media_id: mediaIdString
            });
        };
        console.log('status: ' + status)
        return status;
    }).then((response) => {
        console.log(`STATUS \t response ==> ${response}`)

        //  STEP 4 (FINALIZE)
        return client.post('media/upload', {
            command: 'FINALIZE',
            media_id: mediaIdString
        });
    });
}

And here’s twit:

var Twit = require('twit')
var T = new Twit({
    consumer_key: '...',
    consumer_secret: '...',
    access_token: '...',
    access_token_secret: '...',
    timeout_ms:           60*1000,
    strictSSL:            false,
});

var filePath = '/mnt/4ADE1465DE144C17/Videos/video.mp4'

function upload(file) {
    return new Promise((resolve, reject) => {
        T.postMediaChunked({file_path: file}, function(err, data, response) {
            if (err) {
                reject(err);
            } else {
                resolve(data);
            }
        });
    });
}

upload(filePath).then(
    (data) => {
        console.log(data.media_id_string);
        T.post('statuses/update', {
            status: 'test',
            media_ids: [data.media_id_string]
        });
    },
    (err) => {
        console.log(err);
    }
);

I had success uploading this same file with python. I chose an old random gameplay video under 25MB and it worked

Thanks for the link! I’ll try and fiddle with this test and post any progress asap