Posts tagged with instagram-graph-api

Hey guys so im trying to automate the post on intagram in my app using the api graph from facebook, i already got the single post of pictures and videos of reels, stories, and post, got as well a carousel of pictures but im having a lot of problem trying to make works to POST a carousel that contais or just reels (videos) and mixed onde having reels and pictures, most of the time i pass the process of creating the container for the first video or picture (when mixed) but in the second one usully or i get a timeout or a erro from graph that dont specify nothing or it fails to create the 2 container and pass the status finished, if anyone can help me would be much appreciated ! my code below !

 function postOnInstagram($fb, $message, $mediaUrls = [], $isCarrosel = false, $pageAccessToken, $instagramAccountId, $hasVideo = false, $typePost){   $fb->setDefaultAccessToken($pageAccessToken);   try {       switch ($typePost) {           case 'post':               if ($hasVideo) {                   return $isCarrosel                        ? postInstagramMediaCarousel($fb, $instagramAccountId, $message, $mediaUrls)                       : postInstagramSingleVideo($fb, $instagramAccountId, $message, $mediaUrls[0]);               } else {                   return $isCarrosel                        ? postInstagramPhotoCarousel($fb, $instagramAccountId, $message, $mediaUrls)                       : postInstagramSinglePhoto($fb, $instagramAccountId, $message, $mediaUrls[0]);               }           case 'reel':               if (!$hasVideo) {                   throw new Exception("Reels require a video");               }               if ($mediaUrls > 1) {                 return postInstagramMultipleVideos($fb, $instagramAccountId, $message, $mediaUrls);               }               else {                 return postInstagramReel($fb, $instagramAccountId, $message, $mediaUrls[0]);               }           case 'story':               if ($hasVideo) {                   return postInstagramStoryVideo($fb, $instagramAccountId, $message, $mediaUrls[0]);               } elseif (!empty($mediaUrls)) {                   return postInstagramStoryPhoto($fb, $instagramAccountId, $message, $mediaUrls[0]);               } else {                   return postInstagramStoryText($fb, $instagramAccountId, $message);               }           default:               throw new Exception("Invalid post type");       }   } catch (FacebookResponseException $e) {       return 'Graph returned an error: ' . $e->getMessage();   } catch (FacebookSDKException $e) {       return 'Facebook SDK returned an error: ' . $e->getMessage();   } catch (Exception $e) {       return 'General error: ' . $e->getMessage();   } } function postInstagramMediaCarousel($fb, $instagramAccountId, $message, $mediaUrls){     $mediaIds = [];     foreach ($mediaUrls as $mediaUrl) {         sleep(10);         // Detect media type based on URL or other means         $mediaType = (preg_match('/\.(mp4)$/i', $mediaUrl)) ? 'VIDEO' : 'IMAGE';         // Create the media and get the creation ID         $creationId = createMediaInsta($fb, $instagramAccountId, $mediaUrl, $mediaType);         if (!$creationId) {             throw new Exception("Failed to create media: $mediaUrl");         }         // Wait for the media to be ready         if (!waitForMediaToBeReadyInsta($fb, $creationId)) {             throw new Exception("Media is not ready: $creationId");         }         $mediaIds[] = $creationId;     }         // Create carousel with the obtained media IDs     $carouselResponse = $fb->post("/$instagramAccountId/media", [         'caption' => $message,         'media_type' => 'CAROUSEL',         'children' => $mediaIds,     ]);       $carouselCreationId = $carouselResponse->getDecodedBody()['id'];     if (!$carouselCreationId) {         throw new Exception("Failed to create carousel.");     }     // Wait for the carousel to be ready     if (!waitForMediaToBeReadyInsta($fb, $carouselCreationId)) {         throw new Exception("Carousel is not ready: $carouselCreationId");     }     sleep(30);     // Publish the carousel     return publishMedia($fb, $instagramAccountId, $carouselCreationId); } function createMediaInsta($fb, $instagramAccountId, $mediaUrl, $mediaType){     // Configura o payload dependendo do tipo de mídia     $payload = [         'media_type' => $mediaType,         'is_carousel_item' => 'true'     ];     if ($mediaType === 'VIDEO') {         $payload['video_url'] = $mediaUrl;     } else {         $payload['image_url'] = $mediaUrl;     }     $response = $fb->post("/$instagramAccountId/media", $payload);     return $response->getDecodedBody()['id']; } function waitForMediaToBeReadyInsta($fb, $creationId, $timeout = 300, $interval = 10){     $start = time();     do {         sleep($interval);         try {             $response = $fb->get("/$creationId");             $status = $response->getDecodedBody()['status'];         } catch (Exception $e) {             // Log the exception and continue waiting             error_log("Exception: " . $e->getMessage());             return false;         }         if ($status === 'READY') {             return true;         }         if (time() - $start > $timeout) {             error_log("Timeout: Media not ready in time.");             return false;         }     } while (true); } function publishMedia($fb, $instagramAccountId, $creationId){   return $fb->post("/$instagramAccountId/media_publish", [       'creation_id' => $creationId   ])->getDecodedBody(); } 

i tried everything that i could find on docs and net but cant find a solution!

I hope your day is going well. When I make a request using this endpoint:

  url = "https://graph.instagram.com/access_token"   params = {       "grant_type": "ig_exchange_token",       "client_secret": client_secret,       "access_token": short_lived_access_token   } 

I encounter the following error:

{'error': {'message': 'Unsupported request - method type: post', 'type': 'IGApiException', 'code': 100, 'fbtrace_id': 'Ape_-PbPFO_idh_eqtMB3jz'}} 

I'm unsure of the problem. I have added a verified business, but I have not submitted my app for app review. Should I do that first?

Additionally, it's not just this endpoint; I encounter issues with every endpoint I attempt to request, although I can obtain the short-lived access token without any issues.

I tried to exchange short lived token for the long lived once, and I was expecting to recieve the long lived token

I would like to call for suggestions regarding a challenge I've been trying to solve recently involving Meta API & Webhooks. I assume this particular setting has not been addressed here before.

As one may know, to configure receiving requests from WhatsApp, Facebook, and Instagram for a Meta app you need setting up a webhook for each in the respective parts of Meta dev dashboard. This involves allowing webhooks required app-level subscriptions/permissions and providing an endpoint that you confirm by returning a verify token on a GET request to ensure it's ok to send you requests there.

In my case, I would like other people to use the app as well, so I added oath2 to get necessary permissions for user business pages on Wa, Fb or Instagram, which works well.

After that, with necessary access, you can add your or someone's page to a pre-configured webhook, so it starts accepting requests. This action can be done through a dashboard in the menu that looks similar to this depending on the platform.

On the other hand, this can be done with sending a request, specifying webhook subscription fields yourself (such as messages on the picture above).

POST /v20.0/{app-id}/subscriptions HTTP/1.1

However, this feature is only available for Facebook pages and in the request-dedicated article we can see it mentioned. Any other webhook-related requests actually allowed for WhatsApp and Instagram are only for adding a webhook to an app (the very first step described here).

Limitations:

Webhooks for Instagram is not supported. Instagram webhooks must be configured using the App Dashboard. Webhooks for WhatsApp is not supported. WhatsApp webhooks must be configured using the App Dashboard. https://developers.facebook.com/docs/graph-api/reference/v20.0/app/subscriptions

And given that, one needs to do it through dev dashboard UI which is not an acceptable solution in my case.

Therefore, I can really use some insights on how can this be solved directly or through some witty alternative to receive messages from all three platforms and being able to programmatically allow webhook page requests.

Additionally, I have been considering raising the issue with Meta directly, so it can be adjusted and would love to hear thoughts on potentially doing this.

P.S.

For those deeply interested in the issue, I found some sort of similar issue on the meta dev forum https://developers.facebook.com/community/threads/799110895741478/

We developed our own platform to schedule Instagram Posts via the Instagram Graph API.

It's working fine but we can't find a way to add linebreaks to the Reel Description.

We tried to use \n as well as some html tags like <p> but nothing works. It's like after publication, Instagram automatically deletes the spaces and reduces them to one space.

I know it's possible because some scheduling SAAS like Later can schedule Reels with linebreaks in the description.

I'm trying to find someone who solved the problem already. What should I try next? How to format the json?

Thanks, T.

I am trying to figure out how to approve an Instagram app which use instagram_basic and instagram_manage_messages.

I have created an app that offers to my customers a system to order and choose a Pick & Collect solution in my shops.

The app is working well in development mode but I keep getting rejection by Facebook Review team and I can't figure out why.

For instagram_basic, I have added this video: https://youtu.be/3kTJw-xegsY

And for instagram_manage_messages, this one: https://youtu.be/jvzUNG4q9qU

Any help would be greatly appreciated!