Reputation: 9
I am trying to make an application that allows user to upload images and videos posts to the facebook page by uploading them to the application. My access token and all other stuffs are working fine for uploading pictures, but not for videos.
const [file, setFile] = useState([]); code for uploading picture (working) const formData = new FormData();
formData.append("source", file)
axios.post("https://graph.facebook.com/<pageId>/photos?",
formData,
{
headers:
{
"Content-Type": "multipart/form-data",
"Authorization": "Bearer <access_token>"
}
}
).then(
(res) => {
if (res.data["id"]) {
alert("Upload Successfully")
}
}
).catch((err) => {
alert("Error communicating to server")
console.log(err)
})
code for uploading video (not working)
const formData = new FormData();
formData.append("file", file);
axios.post("https://graph-video.facebook.com/v16.0/<page_id>/videos",
formData,
{
headers:
{
"Content-Type": "multipart/form-data",
"Authorization": "Bearer <same_access_token>"
}
}
).then(
(res) => {
if (res.data["id"]) {
alert("Upload Successfully")
}
}
).catch((err) => {
alert("Error communicating to server")
console.log(err)
})
code for getting input <input className="btn" type="file" accept="video/mp4,video/x-m4v,video/*" onChange= { (e) => { handleChange(e) } } />
<input className="btn" type="file" accept="image/png, image/jpeg" onChange= { (e) => { handleChange(e) }
} />
const handleChange = (e) => {
setFile(e.target.files[0]);
}
Upvotes: 0
Views: 184
Reputation: 1
i use me in uri because the page token is genarated for specific page, it's also work with page id. i done this in node js here is code and it's working.
const form = new FormData();
form.append('access_token', `${pageAccessToken}`); //use your access token
form.append('file_url', `${reqData.image[0]}`); //add video url here
form.append('description', `${reqData.message}`); //add post title here
const videoUpload = await axios.post(
'https://graph-video.facebook.com/v15.0/me/videos', // you can use page id insted of me
form, {
headers: {
...form.getHeaders()
}
}
);
if(videoUpload.status == 200){
console.log("video uploaded")
}else{
console.log("error")
}
Upvotes: 0