Can not upload video file node.js to facebook graph api version v21,0 and error is video file not supported
Can not upload video file node.js to facebook graph api version v21,0 and error is video file not supported
I am Creating a ecommerce what will be able to post facebook the video events of the website .
and I am using facebook graph api version v21.0
for generating session id , I use
//FACEBOOK_GRAPH_API=https://graph.facebook.com //FACEBOOK_GRAPH_VERSION=v21.0 async function initInializeVideoUploadSession({file_name,file_length,file_type,access_token}) { let url =makeUrlWithParams(`${FACEBOOK_GRAPH_API}/${FACEBOOK_GRAPH_VERSION}/${FACEBOOK_APP_ID}/uploads`,{ file_name, file_length, file_type, access_token }); log({url}) let res=await fetch(url, { method :'POST' }); res=await res.json(); if (!res.id) { console.log(res); throw 'Can not facebook token' } return res.id }
for generating the file handle
//FACEBOOK_GRAPH_API=https://graph.facebook.com //FACEBOOK_GRAPH_VERSION=v21.0 async function uploadAVideoFile(options) { let prom= new Promise( async(resolve, reject) => { let { session, access_token, videoPath, filename }=options let url =makeUrlWithParams(`${FACEBOOK_GRAPH_API}/${FACEBOOK_GRAPH_VERSION}/${session}`,{}) log({url}) request(url , { method :'POST', headers :{ 'Authorization':'OAuth '+access_token, 'file_offset' :'0', 'accapt':"video/mp4", 'cache-control': 'no-cache', 'content-type' : 'video/mp4', 'content-disposition': 'attachment; filename=' + filename, 'content-length':fs.statSync(videoPath).size.toString() }, body :fs.readFileSync(videoPath,'binary'), encoding :null },responseCallBack) function responseCallBack(error , response, data) { data=JSON.parse(data); log(data) if (data.h) return resolve(data.h) if (!data.h) { throw new Error('File Handler is not define') } } }) let h=await prom.then(h => h) return h }
By facebook api, I can generate session id ,file handle ,
but when I am using this file handle to post a video on facebook page i am getting error ,video format not supported,please see the upload video code
async function videoFacebookApi(req,res) { let {title,description,filename}=req.body; try { if (typeof title !== 'string' ) throw 'title is null' if (typeof description !== 'string' ) throw 'description is null' if (typeof filename !== 'string' ) throw 'filename is null' let access_token = await settingsAsString('fb_access_token'); let tokenDate = await settingsAsString('fb_access_token_enroll_date'); if ( !access_token || !tokenDate) { throw new Error('!FV_PAGE_ACCESS_TOKEN || !tokenDate') } updateFacebookApiAccessToken(tokenDate); let videoPath=resolve(dirname(fileURLToPath(import.meta.url)), '../../../public/video/Beauty.mp4'); log('// file upload session started') let session=await initInializeVideoUploadSession({ file_name:filename, file_length:fs.statSync(videoPath).size, file_type:"video/mp4", access_token }); log('// file uploading started') let file_handle= await uploadAVideoFile({ session, access_token, videoPath, filename, }); log('// video post started') await request.post(makeUrlWithParams('https://graph-video.facebook.com'+'/'+FACEBOOK_GRAPH_VERSION+'/'+FB_PAGE_ID+'/videos',{}), { headers :{ "Content-Type": "multipart/form-data", "accept": "*/*" }, formData :{ title, description, access_token, fbuploader_video_file_chunk:file_handle } }, uploadToFacebook ); async function uploadToFacebook(error, response, body) { try { if (error) { console.error(error) return res.sendStatus(500) } if (body) { body=await JSON.parse(body) if (body.id) { console.log({body}); console.log('//video upload completed'); return res.sendStatus(201) } if (body.error) { console.error({...body.error}); return res.sendStatus(400) } console.log({body}); return res.sendStatus(200) } return res.sendStatus(200) } catch (error) { console.log({error}); return res.sendStatus(500) } } } catch (error) { log({error}) return res.sendStatus(500) } }
Please see the error carefully
{ message: "The video file you selected is in a format that we don't support.", type: 'OAuthException', code: 352, error_subcode: 1363024, is_transient: false, error_user_title: 'Unsupported Video Format', error_user_msg: "The video you're trying to upload is in a format that isn't supported. Please try again with a video in a supported format.", fbtrace_id: 'ASMNMrVVh4jeY06_nDP_y9d' }
then docomentation I am following for this functionality is facebook graph api docomentation