Tuesday, 31 August 2021

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"
    },
    {
      "id": "44444444444444444"
    },
    {
      "id": "55555555555555555"
    },
    {
      "id": "66666666666666666"
    },
    {
      "id": "77777777777777777"
    },
    {
      "id": "88888888888888888"
    },
    {
      "id": "99999999999999999"
    },
    {
      "id": "00000000000000000"
    },
    {
      "id": "AAAAAAAAAAAAAAAAA"
    },
    {
      "id": "BBBBBBBBBBBBBBBBB"
    },
    {
      "id": "CCCCCCCCCCCCCCCCC"
    },
    {
      "id": "DDDDDDDDDDDDDDDDD"
    },
    {
      "id": "EEEEEEEEEEEEEEEEE"
    },
    {
      "id": "FFFFFFFFFFFFFFFFF"
    },
    {
      "id": "GGGGGGGGGGGGGGGGG"
    },
    {
      "id": "HHHHHHHHHHHHHHHHH"
    },
    {
      "id": "IIIIIIIIIIIIIIIII"
    },
    {
      "id": "JJJJJJJJJJJJJJJJJ"
    },
    {
      "id": "KKKKKKKKKKKKKKKKK"
    },
    {
      "id": "LLLLLLLLLLLLLLLLL"
    },
    {
      "id": "MMMMMMMMMMMMMMMMM"
    },
    {
      "id": "NNNNNNNNNNNNNNNNN"
    },
    {
      "id": "OOOOOOOOOOOOOOOOO"
    }
  ],
  "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.

If you have knowledge about the Instagram API and can help me with an additional one, I would appreciate it. That would be in regards to how I can do to automatically update the token, because it expires in a few hours and I would like to update it automatically.



from Post image to Instagram from a Javascript using Instagram API

No comments:

Post a Comment