Search code examples
javascriptgoogle-apps-scriptfacebook-graph-apiinstagram-apiinstagram-graph-api

Post image to Instagram from a Javascript using Instagram API


Instagram Graph API:
https://developers.facebook.com/docs/instagram-api/

Content Publishing:
https://developers.facebook.com/docs/instagram-api/guides/content-publishing/

My code Javascript in Google App Script:

function InstagramPost() {

  const id = '123456789';
  const image = 'https://www.w3schools.com/images/w3schools_green.jpg';
  const text = 'Hello%20World';
  const access_token = 'TESTTESTTESTTESTTESTTESTTESTTESTTESTTESTTESTTESTTESTTESTTESTTESTTESTTEST';
  const container = 'https://graph.facebook.com/v11.0/' + id + '/media?image_url=' + image + '&caption=' + text + '&access_token=' + access_token;

  const response = UrlFetchApp.fetch(container);
  const creation = response.getContentText();

  Logger.log(creation);
}

The return in my Logger of my container to Post via Instagram API request comes as follows:

{
  "data": [
    {
      "id": "11111111111111111"
    },
    {
      "id": "22222222222222222"
    },
    {
      "id": "33333333333333333"
    }
  ],
  "paging": {
    "cursors": {
      "before": "QWOURQWNGEWRONHWENYWPETGNWQPGNPGNWEPGNWEPGNWEPNGWPENGPWEG",
      "after": "WIWEPGNEPBNWE´GNÉ´BNWE´BNWÉBWNEB´WENBNWEBWEBEWBWE"
    },
    "next": "https://graph.facebook.com/v11.0/11111111111111111/media?access_token=PQWNFWPQINPWNBQPWNBQPWNBPQWNVQWPNVPQWVNPQWPVNQPWNVQPWVNQPWNVPQWNVQPWNVQPWVNQASASLGÇAJKSGLJAAÇSNAÇKNSVÇLKNASBÇANSBÇAS"
  }
}

To make the final call for post image it is necessary to use an creation_id=:

const sendinstagram = 'https://graph.facebook.com/v11.0/' + id + '/media_publish?creation_id=' + creation + '&access_token=' + access_token;
UrlFetchApp.fetch(sendinstagram);

If the return from the container is several id in sequence, how do I know which one to define for the call?

Note: I can't loop to try every id because Instagram has a daily limit of 25 calls and posts, so if I did that I would end up with my calls just trying to post a single image.


Solution

  • I found the correct way to publish images:

      var formData = {
        'image_url': image,
        'caption': text,
        'access_token': access_token
      };
      var options = {
        'method' : 'post',
        'payload' : formData
      };
      const container = 'https://graph.facebook.com/v11.0/' + instagram_business_account + '/media';
    
      const response = UrlFetchApp.fetch(container, options);
    
      const creation = response.getContentText();
      var data = JSON.parse(creation);
      var creationId = data.id
      var formDataPublish = {
          'creation_id': creationId,
          'access_token': access_token
      };
      var optionsPublish = {
        'method' : 'post',
        'payload' : formDataPublish
      };
      const sendinstagram = 'https://graph.facebook.com/v11.0/' + instagram_business_account + '/media_publish';
      
      UrlFetchApp.fetch(sendinstagram, optionsPublish);