【发布时间】:2019-01-26 11:32:41
【问题描述】:
我正在使用 Twitter npm 包制作一个简单的应用程序,可以将 gif 或视频上传到平台。在 FINALIZE 步骤中出现 400 Bad Request 错误。 我不明白为什么我会收到这个错误。我将我的代码构建为包和 Twitter 文档提供的示例。在堆栈溢出和 Twitter 论坛上没有太多关于此错误的讨论,他们说如果媒体文件未通过验证,则会产生此错误,但他们没有指定验证。如果有人能解释为什么会这样,那将是一个很大的帮助。
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
})
})
}
【问题讨论】: