【问题标题】:Getting HTTP Error: 400 Bad Request on GIF or video upload on Twitter收到 HTTP 错误:GIF 或 Twitter 上的视频上传时出现 400 错误请求
【发布时间】: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
        })
    })
}

【问题讨论】:

    标签: node.js twitter


    【解决方案1】:

    第一个问题是导致您获得Error: HTTP Error: 400 Bad Request 是您通过fs.readFileSync 获得的数据是原始二进制文件,而不是base64 编码文件。 Twitter 的 API 参考指出:

    media :上传的原始二进制文件内容。不能与media_data一起使用

    media_data :正在上传的 base64 编码文件内容。不能与media一起使用

    POST media/upload > Parameters

    所以你应该使用media参数来传递文件而不是media_data

    您的 promise chain 在尝试使用 chunked-upload 方法 时的第二个问题有点问题,实际上应该如下所示:

    client.post('media/upload', {
      command: 'INIT',
      total_bytes: gifSize,
      media_type: contentType
    }).then((response) => {
      mediaIdString = response.media_id_string;
      client.post('media/upload', {
        command: 'APPEND',
        media_id: mediaIdString,
        media: gifFile,
        segment_index: 0
      }).then((response) => {
        client.post('media/upload', {
          command: 'FINALIZE',
          media_id: mediaIdString,
        }).then((response) => {
          console.log(response);
          console.log('UPLOAD SUCCESSFUL');
        });
      });
    });
    

    我认为您尝试编写 promise 瀑布,但我认为它在这种情况下效果不佳,或者如果它有效,则需要编写更多代码,并且在全部。

    另外,twitter 包的README.md 中没有提到STATUS 命令,所以这个库似乎不支持STATUS

    【讨论】:

    • 你说得对,我应该将media_database64 编码文件一起使用,mediaraw 文件 一起使用,我知道,我因为沮丧而弄乱了代码,并在没有事先运行它的情况下发布了问题。而且编写代码的方法不会产生任何错误,我只是避免嵌套承诺。我认为问题在于 STATUS 步骤。将 media_data 更改为 media 并删除 STATUS 步骤后,代码运行良好。谢谢您的回复。
    • @neer17 很高兴能帮上忙。
    • 我认为这个 Twitter 包不支持 STATUS 步骤,如果您可以在答案中包含此步骤,那么我很乐意接受。
    • @neer17 当然,我已经添加了。
    猜你喜欢
    • 1970-01-01
    • 2011-09-11
    • 1970-01-01
    • 1970-01-01
    • 2019-11-12
    • 2015-10-20
    • 1970-01-01
    • 1970-01-01
    • 2012-10-04
    相关资源
    最近更新 更多