I am using the TIKTOK Content Posting API to upload the video to TIKTOK. It is working fine for the videos less than 64MB size but when the video is more than 64MB then it is showing the error on the server.
My node js code is as follows
await downloadFile(videoURL, tempDownloadPath);
const videoStats = fs.statSync(tempDownloadPath);
const videoSize = videoStats.size;
const MIN_CHUNK_SIZE = 5 * 1024 * 1024; // 5 MB
const MAX_CHUNK_SIZE = 64 * 1024 * 1024; // 10 MB - Change this back to 64
let chunkSize = Math.min(MAX_CHUNK_SIZE, videoSize);
if (videoSize < MIN_CHUNK_SIZE) {
chunkSize = videoSize;
}
const totalChunkCount = Math.ceil(videoSize / chunkSize);
let data = {
post_info: {
title: tiktokvideoTitle,
privacy_level: tiktokaccessSpecifier,
disable_duet: false,
disable_comment: false,
disable_stitch: false,
video_cover_timestamp_ms: 1000,
brand_content_toggle: false,
brand_organic_toggle: false
},
source_info: {
source: "FILE_UPLOAD",
video_size: videoSize,
chunk_size: chunkSize,
total_chunk_count: totalChunkCount
}
};
console.log("data =====", data);
const initResponse = await axios.post('https://open.tiktokapis.com/v2/post/publish/video/init/', data, {
headers: {
'Authorization': 'Bearer ' + accessToken,
'Content-Type': 'application/json; charset=UTF-8'
}
}).catch(err => {
console.error('Error initializing video upload:', err);
throw new Error('Failed to initialize video upload');
});
I am getting the following error on server
0|socialUploader | Error initializing video upload: AxiosError: Request failed with status code 400
0|socialUploader | at settle (/home/socialuploader-backend/node_modules/axios/dist/node/axios.cjs:1967:12)
0|socialUploader | at IncomingMessage.handleStreamEnd (/home/ssemble/socialuploader-backend/node_modules/axios/dist/node/axios.cjs:3062:11)
0|socialUploader | at IncomingMessage.emit (node:events:531:35)
0|socialUploader | at endReadableNT (node:internal/streams/readable:1696:12)
0|socialUploader | at process.processTicksAndRejections (node:internal/process/task_queues:82:21) {
0|socialUploader | code: 'ERR_BAD_REQUEST',
0|socialUploader | config: {
0|socialUploader | transitional: {
0|socialUploader | silentJSONParsing: true,
0|socialUploader | forcedJSONParsing: true,
0|socialUploader | clarifyTimeoutError: false
0|socialUploader | },
0|socialUploader | adapter: [ 'xhr', 'http' ],
0|socialUploader | transformRequest: [ [Function: transformRequest] ],
0|socialUploader | transformResponse: [ [Function: transformResponse] ],
0|socialUploader | timeout: 0,
0|socialUploader | xsrfCookieName: 'XSRF-TOKEN',
0|socialUploader | xsrfHeaderName: 'X-XSRF-TOKEN',
0|socialUploader | maxContentLength: -1,
0|socialUploader | maxBodyLength: -1,
0|socialUploader | env: { FormData: [Function], Blob: [class Blob] },
0|socialUploader | validateStatus: [Function: validateStatus],
0|socialUploader | headers: Object [AxiosHeaders] {
0|socialUploader | Accept: 'application/json, text/plain, */*',
0|socialUploader | 'Content-Type': 'application/json; charset=UTF-8',
0|socialUploader | Authorization: 'Bearer act.whzQnXJIBlaAoNXinygTmE4N7iJWu7uYNAvh8TfXxJqAdqETHJ3snkJexaeT!5654.e1',
0|socialUploader | 'User-Agent': 'axios/1.6.3',
0|socialUploader | 'Content-Length': '476',
0|socialUploader | 'Accept-Encoding': 'gzip, compress, deflate, br'
0|socialUploader | },
0|socialUploader | method: 'post',
0|socialUploader | url: 'https://open.tiktokapis.com/v2/post/publish/video/init/',
0|socialUploader | data: `{"post_info":{"title":"Unravel the 'nice guy' myth. Discover the truth behind being genuine and attractive without getting walked over. #NiceGuys #DatingAdvice #SelfRespect","privacy_level":"PUBLIC_TO_EVERYONE","disable_duet":false,"disable_comment":false,"disable_stitch":false,"video_cover_timestamp_ms":1000,"brand_content_toggle":false,"brand_organic_toggle":false},"source_info":{"source":"FILE_UPLOAD","video_size":103107045,"chunk_size":67108864,"total_chunk_count":2}}`
0|socialUploader | },
0|socialUploader | request: <ref *1> ClientRequest {
0|socialUploader | _events: [Object: null prototype] {
0|socialUploader | abort: [Function (anonymous)],
0|socialUploader | aborted: [Function (anonymous)],
0|socialUploader | connect: [Function (anonymous)],
0|socialUploader | error: [Function (anonymous)],
0|socialUploader | socket: [Function (anonymous)],
0|socialUploader | timeout: [Function (anonymous)],
0|socialUploader | finish: [Function: requestOnFinish]
0|socialUploader | },
0|socialUploader | _eventsCount: 7,
0|socialUploader | _maxListeners: undefined,
0|socialUploader | outputData: [],
0|socialUploader | outputSize: 0,
0|socialUploader | writable: true,
0|socialUploader | destroyed: false,
0|socialUploader | _last: false,
0|socialUploader | chunkedEncoding: false,
0|socialUploader | shouldKeepAlive: false,
0|socialUploader | maxRequestsOnConnectionReached: false,
0|socialUploader | _defaultKeepAlive: true,
0|socialUploader | useChunkedEncodingByDefault: true,
0|socialUploader | sendDate: false,
0|socialUploader | _removedConnection: false,
0|socialUploader | _removedContLen: false,
0|socialUploader | _removedTE: false,
0|socialUploader | strictContentLength: false,
0|socialUploader | _contentLength: '476',
0|socialUploader | _hasBody: true,
0|socialUploader | _trailer: '',
0|socialUploader | finished: true,
0|socialUploader | _headerSent: true,
0|socialUploader | _closed: false,
0|socialUploader | socket: TLSSocket {
0|socialUploader | _tlsOptions: [Object],
0|socialUploader | _secureEstablished: true,
0|socialUploader | _securePending: false,
0|socialUploader | _newSessionPending: false,
0|socialUploader | _controlReleased: true,
0|socialUploader | secureConnecting: false,
0|socialUploader | _SNICallback: null,
0|socialUploader | servername: 'open.tiktokapis.com',
0|socialUploader | alpnProtocol: false,
0|socialUploader | authorized: true,
0|socialUploader | authorizationError: null,
0|socialUploader | encrypted: true,
0|socialUploader | _events: [Object: null prototype],
0|socialUploader | _eventsCount: 10,
0|socialUploader | connecting: false,
0|socialUploader | _hadError: false,
0|socialUploader | _parent: null,
0|socialUploader | _host: 'open.tiktokapis.com',
0|socialUploader | _closeAfterHandlingError: false,
0|socialUploader | _readableState: [ReadableState],
0|socialUploader | _writableState: [WritableState],
0|socialUploader | allowHalfOpen: false,
0|socialUploader | _maxListeners: undefined,
0|socialUploader | _sockname: null,
0|socialUploader | _pendingData: null,
0|socialUploader | _pendingEncoding: '',
0|socialUploader | server: undefined,
0|socialUploader | _server: null,
0|socialUploader | ssl: [TLSWrap],
0|socialUploader | _requestCert: true,
0|socialUploader | _rejectUnauthorized: true,
0|socialUploader | timeout: 5000,
0|socialUploader | parser: null,
0|socialUploader | _httpMessage: [Circular *1],
0|socialUploader | autoSelectFamilyAttemptedAddresses: [Array],
0|socialUploader | [Symbol(alpncallback)]: null,
0|socialUploader | [Symbol(res)]: [TLSWrap],
0|socialUploader | [Symbol(verified)]: true,
0|socialUploader | [Symbol(pendingSession)]: null,
0|socialUploader | [Symbol(async_id_symbol)]: 22235383,
0|socialUploader | [Symbol(kHandle)]: [TLSWrap],
0|socialUploader | [Symbol(lastWriteQueueSize)]: 0,
0|socialUploader | [Symbol(timeout)]: Timeout {
0|socialUploader | _idleTimeout: 5000,
0|socialUploader | _idlePrev: [TimersList],
0|socialUploader | _idleNext: [Timeout],
0|socialUploader | _idleStart: 178692186,
0|socialUploader | _onTimeout: [Function: bound ],
0|socialUploader | _timerArgs: undefined,
0|socialUploader | _repeat: null,
0|socialUploader | _destroyed: false,
0|socialUploader | [Symbol(refed)]: false,
0|socialUploader | [Symbol(kHasPrimitive)]: false,
0|socialUploader | [Symbol(asyncId)]: 22186526,
0|socialUploader | [Symbol(triggerId)]: 22186524
0|socialUploader | },
0|socialUploader | [Symbol(kBuffer)]: null,
0|socialUploader | [Symbol(kBufferCb)]: null,
0|socialUploader | [Symbol(kBufferGen)]: null,
0|socialUploader | [Symbol(shapeMode)]: true,
0|socialUploader | [Symbol(kCapture)]: false,
0|socialUploader | [Symbol(kSetNoDelay)]: false,
0|socialUploader | [Symbol(kSetKeepAlive)]: true,
0|socialUploader | [Symbol(kSetKeepAliveInitialDelay)]: 60,
0|socialUploader | [Symbol(kBytesRead)]: 0,
0|socialUploader | [Symbol(kBytesWritten)]: 0,
0|socialUploader | [Symbol(connect-options)]: [Object]
0|socialUploader | },
0|socialUploader | _header: 'POST /v2/post/publish/video/init/ HTTP/1.1rn' +
0|socialUploader | 'Accept: application/json, text/plain, */*rn' +
0|socialUploader | 'Content-Type: application/json; charset=UTF-8rn' +
0|socialUploader | 'Authorization: Bearer act.whzQnXJIBlaAoNXinygTmE4N7iJWu7uYNAvh8TfXxJqAdqETHJ3snkJexaeT!5654.e1rn' +
0|socialUploader | 'User-Agent: axios/1.6.3rn' +
0|socialUploader | 'Content-Length: 476rn' +
0|socialUploader | 'Accept-Encoding: gzip, compress, deflate, brrn' +
0|socialUploader | 'Host: open.tiktokapis.comrn' +
0|socialUploader | 'Connection: keep-alivern' +
0|socialUploader | 'rn',
0|socialUploader | _keepAliveTimeout: 0,
0|socialUploader | _onPendingData: [Function: nop],
0|socialUploader | agent: Agent {
0|socialUploader | _events: [Object: null prototype],
0|socialUploader | _eventsCount: 2,
0|socialUploader | _maxListeners: undefined,
0|socialUploader | defaultPort: 443,
0|socialUploader | protocol: 'https:',
0|socialUploader | options: [Object: null prototype],
0|socialUploader | requests: [Object: null prototype] {},
0|socialUploader | sockets: [Object: null prototype],
0|socialUploader | freeSockets: [Object: null prototype],
0|socialUploader | keepAliveMsecs: 1000,
0|socialUploader | keepAlive: true,
0|socialUploader | maxSockets: Infinity,
0|socialUploader | maxFreeSockets: 256,
0|socialUploader | scheduling: 'lifo',
0|socialUploader | maxTotalSockets: Infinity,
0|socialUploader | totalSocketCount: 2,
0|socialUploader | maxCachedSessions: 100,
0|socialUploader | _sessionCache: [Object],
0|socialUploader | [Symbol(shapeMode)]: false,
0|socialUploader | [Symbol(kCapture)]: false
0|socialUploader | },
0|socialUploader | socketPath: undefined,
0|socialUploader | method: 'POST',
0|socialUploader | maxHeaderSize: undefined,
0|socialUploader | insecureHTTPParser: undefined,
0|socialUploader | joinDuplicateHeaders: undefined,
0|socialUploader | path: '/v2/post/publish/video/init/',
0|socialUploader | _ended: true,
0|socialUploader | res: IncomingMessage {
0|socialUploader | _events: [Object],
0|socialUploader | _readableState: [ReadableState],
0|socialUploader | _maxListeners: undefined,
0|socialUploader | socket: [TLSSocket],
0|socialUploader | httpVersionMajor: 1,
0|socialUploader | httpVersionMinor: 1,
0|socialUploader | httpVersion: '1.1',
0|socialUploader | complete: true,
0|socialUploader | rawHeaders: [Array],
0|socialUploader | rawTrailers: [],
0|socialUploader | joinDuplicateHeaders: undefined,
0|socialUploader | aborted: false,
0|socialUploader | upgrade: false,
0|socialUploader | url: '',
0|socialUploader | method: null,
0|socialUploader | statusCode: 400,
0|socialUploader | statusMessage: 'Bad Request',
0|socialUploader | client: [TLSSocket],
0|socialUploader | _consuming: true,
0|socialUploader | _dumped: false,
0|socialUploader | req: [Circular *1],
0|socialUploader | _eventsCount: 4,
0|socialUploader | responseUrl: 'https://open.tiktokapis.com/v2/post/publish/video/init/',
0|socialUploader | redirects: [],
0|socialUploader | [Symbol(shapeMode)]: true,
0|socialUploader | [Symbol(kCapture)]: false,
0|socialUploader | [Symbol(kHeaders)]: [Object],
0|socialUploader | [Symbol(kHeadersCount)]: 34,
0|socialUploader | [Symbol(kTrailers)]: null,
0|socialUploader | [Symbol(kTrailersCount)]: 0
0|socialUploader | },
0|socialUploader | aborted: false,
0|socialUploader | timeoutCb: [Function: emitRequestTimeout],
0|socialUploader | upgradeOrConnect: false,
0|socialUploader | parser: null,
0|socialUploader | maxHeadersCount: null,
0|socialUploader | reusedSocket: true,
0|socialUploader | host: 'open.tiktokapis.com',
0|socialUploader | protocol: 'https:',
0|socialUploader | _redirectable: Writable {
0|socialUploader | _events: [Object],
0|socialUploader | _writableState: [WritableState],
0|socialUploader | _maxListeners: undefined,
0|socialUploader | _options: [Object],
0|socialUploader | _ended: true,
0|socialUploader | _ending: true,
0|socialUploader | _redirectCount: 0,
0|socialUploader | _redirects: [],
0|socialUploader | _requestBodyLength: 476,
0|socialUploader | _requestBodyBuffers: [],
0|socialUploader | _eventsCount: 3,
0|socialUploader | _onNativeResponse: [Function (anonymous)],
0|socialUploader | _currentRequest: [Circular *1],
0|socialUploader | _currentUrl: 'https://open.tiktokapis.com/v2/post/publish/video/init/',
0|socialUploader | [Symbol(shapeMode)]: true,
0|socialUploader | [Symbol(kCapture)]: false
0|socialUploader | },
0|socialUploader | [Symbol(shapeMode)]: false,
0|socialUploader | [Symbol(kCapture)]: false,
0|socialUploader | [Symbol(kBytesWritten)]: 0,
0|socialUploader | [Symbol(kNeedDrain)]: false,
0|socialUploader | [Symbol(corked)]: 0,
0|socialUploader | [Symbol(kOutHeaders)]: [Object: null prototype] {
0|socialUploader | accept: [Array],
0|socialUploader | 'content-type': [Array],
0|socialUploader | authorization: [Array],
0|socialUploader | 'user-agent': [Array],
0|socialUploader | 'content-length': [Array],
0|socialUploader | 'accept-encoding': [Array],
0|socialUploader | host: [Array]
0|socialUploader | },
0|socialUploader | [Symbol(errored)]: null,
0|socialUploader | [Symbol(kHighWaterMark)]: 16384,
0|socialUploader | [Symbol(kRejectNonStandardBodyWrites)]: false,
0|socialUploader | [Symbol(kUniqueHeaders)]: null
0|socialUploader | },
0|socialUploader | response: {
0|socialUploader | status: 400,
0|socialUploader | statusText: 'Bad Request',
0|socialUploader | headers: Object [AxiosHeaders] {
0|socialUploader | 'content-type': 'application/json',
0|socialUploader | 'content-length': '126',
0|socialUploader | 'x-janus-info': 'GucksBLCW4ga4RTrAOipfSoEdGQ1f7CQPxXfwQDUQv-0PUVpHehBMf2BH74VA4s6BTJdebptPkQR2ls_-3d9JpWbmmzrbRIzGIrzGG4M9MIJLjt-iuNsuELzR7Z-cIXvu-LrM5h0ad82o44VXc8u51rLkOtkS2LodveVOTzPT0QdrlgSql640NVuVzPnCGjDDTlVt2fg5MTdielv1zjvcmp8uPHdUdaKOriRiLWw_znrd8MawIJX9FC86CC-RFM_4i-gvpg2esp02TSaB5vlXg==',
0|socialUploader | 'x-tt-logid': '2024101905500118C64F950EF28F73F53F',
0|socialUploader | 'x-tt-trace-host': '01eb53e39bc3f3bdb889d4ae199ecc6b05080c020ef3bdd5527279803072ef22810f076faa993027f108408d003f84ba12407c7d4ee53347ca43df8174ee268193967b86e8e9ddab04a0df049b493cc2ce2abac446befa59126a3eb4110054e97fa31a5262ff3e5239ec051ada9b1ca8fb',
0|socialUploader | 'x-tt-trace-id': '00-24101905500118C64F950EF28F73F53F-19D5CB8D2CF8EBC6-00',
0|socialUploader | server: 'TLB',
0|socialUploader | 'x-origin-response-time': '43,104.78.78.101',
0|socialUploader | expires: 'Sat, 19 Oct 2024 05:50:02 GMT',
0|socialUploader | 'cache-control': 'max-age=0, no-cache, no-store',
0|socialUploader | pragma: 'no-cache',
0|socialUploader | date: 'Sat, 19 Oct 2024 05:50:02 GMT',
0|socialUploader | connection: 'close',
0|socialUploader | 'x-tt-trace-tag': 'id=16;cdn-cache=miss;type=dyn',
0|socialUploader | 'server-timing': 'cdn-cache; desc=MISS, edge; dur=30, origin; dur=43, inner; dur=38',
0|socialUploader | 'x-parent-response-time': '67,23.220.105.205'
0|socialUploader | },
0|socialUploader | config: {
0|socialUploader | transitional: [Object],
0|socialUploader | adapter: [Array],
0|socialUploader | transformRequest: [Array],
0|socialUploader | transformResponse: [Array],
0|socialUploader | timeout: 0,
0|socialUploader | xsrfCookieName: 'XSRF-TOKEN',
0|socialUploader | xsrfHeaderName: 'X-XSRF-TOKEN',
0|socialUploader | maxContentLength: -1,
0|socialUploader | maxBodyLength: -1,
0|socialUploader | env: [Object],
0|socialUploader | validateStatus: [Function: validateStatus],
0|socialUploader | headers: [Object [AxiosHeaders]],
0|socialUploader | method: 'post',
0|socialUploader | url: 'https://open.tiktokapis.com/v2/post/publish/video/init/',
0|socialUploader | data: `{"post_info":{"title":"Unravel the 'nice guy' myth. Discover the truth behind being genuine and attractive without getting walked over. #NiceGuys #DatingAdvice #SelfRespect","privacy_level":"PUBLIC_TO_EVERYONE","disable_duet":false,"disable_comment":false,"disable_stitch":false,"video_cover_timestamp_ms":1000,"brand_content_toggle":false,"brand_organic_toggle":false},"source_info":{"source":"FILE_UPLOAD","video_size":103107045,"chunk_size":67108864,"total_chunk_count":2}}`
0|socialUploader | },
0|socialUploader | request: <ref *1> ClientRequest {
0|socialUploader | _events: [Object: null prototype],
0|socialUploader | _eventsCount: 7,
0|socialUploader | _maxListeners: undefined,
0|socialUploader | outputData: [],
0|socialUploader | outputSize: 0,
0|socialUploader | writable: true,
0|socialUploader | destroyed: false,
0|socialUploader | _last: false,
0|socialUploader | chunkedEncoding: false,
0|socialUploader | shouldKeepAlive: false,
0|socialUploader | maxRequestsOnConnectionReached: false,
0|socialUploader | _defaultKeepAlive: true,
0|socialUploader | useChunkedEncodingByDefault: true,
0|socialUploader | sendDate: false,
0|socialUploader | _removedConnection: false,
0|socialUploader | _removedContLen: false,
0|socialUploader | _removedTE: false,
0|socialUploader | strictContentLength: false,
0|socialUploader | _contentLength: '476',
0|socialUploader | _hasBody: true,
0|socialUploader | _trailer: '',
0|socialUploader | finished: true,
0|socialUploader | _headerSent: true,
0|socialUploader | _closed: false,
0|socialUploader | socket: [TLSSocket],
0|socialUploader | _header: 'POST /v2/post/publish/video/init/ HTTP/1.1rn' +
0|socialUploader | 'Accept: application/json, text/plain, */*rn' +
0|socialUploader | 'Content-Type: application/json; charset=UTF-8rn' +
0|socialUploader | 'Authorization: Bearer act.whzQnXJIBlaAoNXinygTmE4N7iJWu7uYNAvh8TfXxJqAdqETHJ3snkJexaeT!5654.e1rn' +
0|socialUploader | 'User-Agent: axios/1.6.3rn' +
0|socialUploader | 'Content-Length: 476rn' +
0|socialUploader | 'Accept-Encoding: gzip, compress, deflate, brrn' +
0|socialUploader | 'Host: open.tiktokapis.comrn' +
0|socialUploader | 'Connection: keep-alivern' +
0|socialUploader | 'rn',
0|socialUploader | _keepAliveTimeout: 0,
0|socialUploader | _onPendingData: [Function: nop],
0|socialUploader | agent: [Agent],
0|socialUploader | socketPath: undefined,
0|socialUploader | method: 'POST',
0|socialUploader | maxHeaderSize: undefined,
0|socialUploader | insecureHTTPParser: undefined,
0|socialUploader | joinDuplicateHeaders: undefined,
0|socialUploader | path: '/v2/post/publish/video/init/',
0|socialUploader | _ended: true,
0|socialUploader | res: [IncomingMessage],
0|socialUploader | aborted: false,
0|socialUploader | timeoutCb: [Function: emitRequestTimeout],
0|socialUploader | upgradeOrConnect: false,
0|socialUploader | parser: null,
0|socialUploader | maxHeadersCount: null,
0|socialUploader | reusedSocket: true,
0|socialUploader | host: 'open.tiktokapis.com',
0|socialUploader | protocol: 'https:',
0|socialUploader | _redirectable: [Writable],
0|socialUploader | [Symbol(shapeMode)]: false,
0|socialUploader | [Symbol(kCapture)]: false,
0|socialUploader | [Symbol(kBytesWritten)]: 0,
0|socialUploader | [Symbol(kNeedDrain)]: false,
0|socialUploader | [Symbol(corked)]: 0,
0|socialUploader | [Symbol(kOutHeaders)]: [Object: null prototype],
0|socialUploader | [Symbol(errored)]: null,
0|socialUploader | [Symbol(kHighWaterMark)]: 16384,
0|socialUploader | [Symbol(kRejectNonStandardBodyWrites)]: false,
0|socialUploader | [Symbol(kUniqueHeaders)]: null
0|socialUploader | },
0|socialUploader | data: { error: [Object] }
0|socialUploader | }
0|socialUploader | }
If someone faced the problem and have solution.
Thanks
I tried different ways to generate the chunks but nothing worked for me.