You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

使用Google App Script调用Instagram API发图如何选取正确creation_id

问题根本原因

你调用创建媒体容器接口时误用了GET请求,这是返回多个ID的核心原因:

  • Instagram Graph API中,GET /{IG用户ID}/media 端点的作用是拉取账号已发布的历史媒体列表,所以会返回多条ID数据和分页参数,和你要的「创建新媒体容器」需求完全不符。
  • 「创建媒体容器」必须使用POST请求调用同一个端点,调用成功后接口只会返回单个ID,就是你需要的creation_id。

修正后的可运行代码

function InstagramPost() {
  const id = '123456789'; // 替换为你的Instagram商家账号ID
  const image = 'https://www.w3schools.com/images/w3schools_green.jpg';
  const text = 'Hello World'; // 不需要手动编码,UrlFetchApp会自动处理特殊字符
  const access_token = '你的有效Access Token';

  // 创建媒体容器的POST请求配置
  const createContainerOptions = {
    method: 'POST',
    payload: {
      image_url: image,
      caption: text,
      access_token: access_token
    }
  };
  const containerEndpoint = `https://graph.facebook.com/v11.0/${id}/media`;

  const response = UrlFetchApp.fetch(containerEndpoint, createContainerOptions);
  const resData = JSON.parse(response.getContentText());
  const creation_id = resData.id; // 这里拿到的就是唯一可用的creation_id

  // 调用发布接口完成内容发布
  const publishOptions = {
    method: 'POST',
    payload: {
      creation_id: creation_id,
      access_token: access_token
    }
  };
  const publishEndpoint = `https://graph.facebook.com/v11.0/${id}/media_publish`;
  const publishRes = UrlFetchApp.fetch(publishEndpoint, publishOptions);
  Logger.log(publishRes.getContentText());
}

注意事项

  • 调用前确保你的Access Token拥有instagram_basic、pages_show_list等必要权限,且账号是已关联Facebook主页的Instagram商家账号。
  • 图片URL必须是公网可访问的无防盗链地址,大小、格式符合Instagram平台要求。
  • 创建容器后不需要额外等待,可直接调用发布接口,API内部会自动处理容器的就绪状态校验。

内容的提问来源于stack exchange,提问作者Digital Farmer

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.10.06 06:48:03