Post 图片从 Javascript 使用 Instagram API

Post image to Instagram from a Javascript using Instagram API

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

内容发布:
https://developers.facebook.com/docs/instagram-api/guides/content-publishing/

我的代码 JavascriptGoogle 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);
}

我容器的 return 通过 Instagram API 请求到 Post 的容器中的 return 如下:

{
  "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"
  }
}

要对 post 图像进行最终调用,必须使用 creation_id=:

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

如果来自容器的 return 是几个 id 顺序,我怎么知道要为调用定义哪一个?

注意:我无法循环尝试每个 ID,因为 Instagram 每天有 25 次调用和 post 次的限制,所以如果我这样做,我最终只会尝试 post 一张图片。

首先,我们通过点击端点来创建 IG 容器。

POST https://graph.facebook.com/v11.0/{ig-user-id}/media?image_url={image-url}&caption={caption}&access_token={access-token}

获得 IG 容器 ID 后,我们将再次向 post 图像发出 POST 请求。

POST https://graph.facebook.com/v11.0/{ig-user-id}/media_publish?creation_id={creation-id}&access_token={access-token}

我认为你必须在 containersendinstagram 中包含版本 v11.0(最新为如果现在)。

我找到了正确的图片发布方式:

  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);