gpt4 book ai didi

node.js - Twitter 视频媒体上传无法正常工作 : Error: HTTP Error: 400 Bad Request

转载 作者:太空宇宙 更新时间:2023-11-04 02:45:43 25 4
gpt4 key购买 nike

我正在尝试使用“twitter”npm 包将视频媒体上传并发布到 Twitter。

在此期间我遇到一个错误:

error  Error: HTTP Error: 400 Bad Request
at Request._callback (/Volumes/Office/Work/Pack3/developer/tweet-it/node_modules/twitter/lib/twitter.js:221:9)
at Request.self.callback (/Volumes/Office/Work/Pack3/developer/tweet-it/node_modules/request/request.js:185:22)
at Request.emit (events.js:180:13)
at Request.<anonymous> (/Volumes/Office/Work/Pack3/developer/tweet-it/node_modules/request/request.js:1157:10)
at Request.emit (events.js:180:13)
at IncomingMessage.<anonymous> (/Volumes/Office/Work/Pack3/developer/tweet-it/node_modules/request/request.js:1079:12)
at Object.onceWrapper (events.js:272:13)
at IncomingMessage.emit (events.js:185:15)
at endReadableNT (_stream_readable.js:1101:12)
at process._tickCallback (internal/process/next_tick.js:114:19)

代码库初始化库

var Twitter = require('twitter');

所有详细信息均通过 Twitter 应用控制台生成。

var twitterClient = new Twitter({
consumer_key: process.env.TWITTER_CONSUMER_KEY,
consumer_secret: process.env.TWITTER_CONSUMER_SECRET,
access_token_key: process.env.TWITTER_ACCESS_TOKEN_KEY,
access_token_secret: process.env.TWITTER_ACCESS_TOKEN_SECRET
});


function makePost(endpoint, params) {
return new Promise(function (resolve, reject) {
twitterClient.post(endpoint, params, function (error, data, response) {
if (error) {
reject(error);
} else {
resolve(data);
}
});
});
}

exports.postVideo = async function (req, res, next) {
var params = req.body;

const pathToMovie = './medias/video_3_640x360_1mb.mp4';
const mediaType = 'video/mp4'; // `'video/mp4'` is also supported
const mediaData = require('fs').readFileSync(pathToMovie);
const mediaSize = require('fs').statSync(pathToMovie).size;
console.log('mediaData ', mediaData);

try {

var options = {
command: 'INIT',
total_bytes: mediaSize,
media_type: mediaType,
};

//该请求即将完成

        var mediaInit = await makePost('media/upload', options);

console.log('options ', options);
console.log('mediaInit ', mediaInit);
console.log('mediaInit.media_id_string ', mediaInit.media_id_string);

//此请求无效。

        await makePost('media/upload', {
command: 'APPEND',
media_id: mediaInit.media_id_string,
media: mediaData,
segment_index: 0
});

var mediaFinalize = await makePost('media/upload', {
command: 'FINALIZE',
media_id: mediaInit.media_id_string,
});
console.log('mediaFinalize ', mediaFinalize);
var status = {
status: params.status,
media_ids: mediaFinalize.media_id_string
};

twitterClient.post('statuses/update', status, function (error, tweet, response) {
if (error) {
return next(error);
}
req.twitterStore = tweet;
return next();
});
} catch (error) {
console.log('error ', error);
return next(error);
}
};

使用的视频: https://www.sample-videos.com/video/mp4/360/big_buck_bunny_360p_1mb.mp4

引用:https://github.com/desmondmorris/node-twitter/tree/master/examples#chunked-media

最佳答案

示例中包含三个上传媒体的命令:

  1. 初始化
  2. 附加
  3. 最终确定

INIT 和 APPEND 命令在您的代码中似乎是正确的,但在 FINALIZE 命令中具有 media_id 作为输入。如果我没有误解代码,您应该使用“mediaInit”对象而不是“media_id_string”设置此参数。

  /**
* Step 2 of 3: Append file chunk
* @param String mediaId Reference to media object being uploaded
* @return Promise resolving to String mediaId (for chaining)
*/
function appendUpload (mediaId) {
return makePost('media/upload', {
command : 'APPEND',
media_id : mediaId,
media : mediaData,
segment_index: 0
}).then(data => mediaId);
}

第二步返回 mediaId,该对象在第三步中使用:

  /**
* Step 3 of 3: Finalize upload
* @param String mediaId Reference to media
* @return Promise resolving to mediaId (for chaining)
*/
function finalizeUpload (mediaId) {
return makePost('media/upload', {
command : 'FINALIZE',
media_id: mediaId <-------------
}).then(data => mediaId);
}

但是在您的代码中,您将“mediaInit.media_id_string”作为参数 media_id 的值传递:

var mediaFinalize = await makePost('media/upload', {
command: 'FINALIZE',
media_id: mediaInit.media_id_string, <-------------
});

这可能是您的代码中的错误。

关于node.js - Twitter 视频媒体上传无法正常工作 : Error: HTTP Error: 400 Bad Request,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50713748/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com