skip to Main Content

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.

2

Answers


  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

    IG content publish troubleshooting

    Login or Signup to reply.
  2. @aib is correct.

    You need to check the status of the media container before calling publish. Meta’s documentation states:

    If you are able to create a container for a video but the POST
    /{ig-user-id}/media_publish endpoint does not return the published
    media ID, you can get the container’s publishing status by querying
    the GET /{ig-container-id}?fields=status_code endpoint.

    It takes some time, depending on the size of the video you are uploading, for the container to be ready for publish. For a 1Mb video, it took me ~10 seconds for the media container to be available. I check for the GET /{ig-container-id}?fields=status_code endpoint to return FINISHED and only then I can finally use the media_publish endpoint with the creationId.

    async function checkIfMediaContainerReady(containerReady) {
      if (!containerReady) {
        console.log('waiting for ig media container to be ready...');
        await sleep(5000);
        let res = await Request(mediaContainerStatusCheckUrl, {method: "GET", headers: headers});
        let statusResponseJson = await JSON.parse(res);
        await checkIfMediaContainerReady(statusResponseJson.status_code === 'FINISHED');
      }
    }
    
    const res = await Request(mediaContainerStatusCheckUrl, {method: "GET", headers: headers});
    const statusResponseJson = await JSON.parse(res);
    await checkIfMediaContainerReady(statusResponseJson.status_code === 'FINISHED');
    
    console.log("ig media container is ready");
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search