Search code examples
javascriptnode.jsexpresstiktok

Sharing Video to TikTok on the Web always return Bad Request


I followed the documentation here https://developers.tiktok.com/doc/web-video-kit-with-web to Log in using the Login Kit and then I successfully got the access_token and open_id for an account.

Now I uploaded a video following the instructions in the documentations as follows from an ExpressJS Server:

const FormData = require('form-data');
const got = require('got');
const axios = require('axios');

//Video Upload Function
async function uploadVideoToTikTok(){
let tikTokAccessToken = //Retrieved from database;
let openId = //Retrieved from database;
let shareUrl = `https://open-api.tiktok.com/share/video/upload/?open_id=${openId}&access_token=${tikTokAccessToken}`;
                
let video = got.stream(mediaUrl); //Media Url is similar to https://firebasestorage.com/adadadadadadad.mp4
let tikTokShareform = new FormData();
tikTokShareform.append("video", video);
try {
     let { data } = await axios.post(shareUrl, tikTokShareform);
     console.log(`TikTok Upload Result=${JSON.stringify(data, null, 2)}`);
   } catch (e) {
    console.log(`TikTok Video Upload Error`);
    console.log(e);
   }
}

But the response I keep getting back is

'Bad Request'

No further info was provided for me to know the reason for the bad request.

Also I noticed the documentation didn't provide any info on how to provide description or title for the video.

Any ideas on how I can resolve this will be truly appreciated.

Thank you


Solution

  • Probably the problem was due to missing Content-Type header in your HTTP POST request, you should try something like:

    let { data } = await axios.post(
      shareUrl,
      {
        headers: {
          "Content-Type": "multipart/form-data",
        },
      },
      tikTokShareform,
    );
    

    Or, even better:

    let { data } = await axios.post(
      shareUrl,
      {
        headers: tikTokShareform.getHeaders(),
      },
      tikTokShareform,
    );