'Video upload to Instagram Business Account always returns "Media ID is not available" error message
I need to publish either a photo or video to an authenticated Instagram Business Account.
I have gotten the access token to the Instagram Business Account and now I wish to upload a video to that account but I keep getting back this error message
Media ID is not available
I followed the instructions to upload either a video or photo from the docs here https://developers.facebook.com/docs/instagram-api/guides/content-publishing/
Please note that the below code works perfectly well at uploading Photos but always fails when it's video.
Here is my code after following their documentation:
let mediaContainerUrl = `https://graph.facebook.com/${igUserId}/media`;
let containerParams = new URLSearchParams();
if (isPhoto) {
containerParams.append('image_url', mediaUrl);
} else {
containerParams.append('video_url', mediaUrl);
containerParams.append('media_type', 'VIDEO');
}
containerParams.append('caption', caption);
containerParams.append('access_token', accessToken);
try {
let mediaContainerResponse = await axios.post(mediaContainerUrl, containerParams);
let { id } = mediaContainerResponse.data;
// id above is supposed to be the creation_id needed for the next step below:
// I can confirm that the id for the created media container was actually returned.
// So, it's weird for me to keep getting the Media ID not available error message.
let mediaPublishResponse = await axios.post(`https://graph.facebook.com/${igUserId}/media_publish?creation_id=${id}&access_token=${access_token}`);
let mediaPublishResponseData = mediaPublishResponse.data;
let publishedMediaId = mediaPublishResponseData.id;
console.log(`File uploaded to Instagram!`);
} catch (e) {
console.log(`Instagram file upload failed miserably`);
console.log(e);
}
Please note that mediaUrl above is coming from a remote url e.g https://somewhere.com/video.mp4
Once again, uploading a Photo with the above code is working perfectly but never works when it's video. I keep getting the following full error message:
OAuth "Facebook Platform" "invalid_request" "Media ID is not available
After careful observation I noticed that indeed the creation_id was generated for the video, so quite frankly, it's weird that I am still getting the above error message.
Please, what could I be doing wrong?
I'm thankful to any suggestions in resolving this.
Solution 1:[1]
So, here is what the problem is. When you upload a video to the /media endpoint, the video takes some time to be processed, and the video will not be available for publishing during this time, hence the reason the /media_publish endpoint is returning "Media Id not available". The solution to this would be to check for the container's publishing status: GET: /{ig-container-id}?fields=status_code. Only when the status_code is "FINISHED" is the container available for publishing. Check the link below for more details
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | aib |
