desmondmorris / node-twitter

Client library for the Twitter REST and Streaming API's.
https://www.npmjs.com/package/twitter
MIT License
1.27k stars 238 forks source link

Getting HTTP Error: 400 Bad Request in the FINALIZE step during GIF upload #319

Closed neer17 closed 5 years ago

neer17 commented 5 years ago

I can't understand why am I getting this error. I have structured my code as the example provided. There is not much discussion about this error on stack overflow and in Twitter forums, they are saying that if a media file doesn't pass validation then this error is produced but they didn't specify the validations.

let fs = require('fs')
let path = require('path')

const keys = require('./config')

const client = new Twitter(keys)

let mediaIdString = ''
let mediaId = 0

//  reading file (GIF)
const gifFile = fs.readFileSync(path.join(__dirname, 'memes', 'gif1.gif'))

//  getting file details
const gifFileStat = fs.statSync(path.join(__dirname, 'memes', 'gif1.gif'))
const gifSize = gifFileStat.size
const contentType = 'image/gif'

console.log(`gif 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)
        return client.get('media/upload', {
            command: 'STATUS',
            media_id: mediaIdString
        })
    }).then((response) => {
        console.log(`STATUS \t response ==> ${response}`)

        //  STEP 4 (FINALIZE)
        return client.post('media/upload', {
            command: 'FINALIZE',
            media_id: mediaIdString
        })
    })
}
mvrozanti commented 5 years ago

Using your code, I get:

Error: HTTP Error: 503 Service Temporarily Unavailable

neer17 commented 5 years ago

After removing the STATUS step the error is gone, I think this step is not supported by the package

mvrozanti commented 5 years ago

After removing STATUS, I get the same error.

slorber commented 3 years ago

FYI status should be called after finalize, and only if there is finalizeResult.processing_info (afaik)