instagram-private-api 洞察响应中的 InsightsServicePostResponseInstagram_actor 接口详解

原创2026-10-15 10:33:301,404 阅读
文章标签:后端

instagram-private-api 洞察响应中的 InsightsServicePostResponseInstagram_actor 接口详解

InsightsServicePostResponseInstagram_actor 是 NodeJS 版 instagram-private-api SDK 中,帖子(Post)数据洞察 响应里用于描述"内容发布主体(Instagram 账户)"的嵌套接口。它只有两个字段:id 与 instagram_actor_id,却承担着将每条帖子的洞察数据(展示量、触达、互动、分享等)归属到具体账号身份的关键职责。读完本文,你将掌握该接口的字段语义、在完整洞察响应树中的位置、底层 GraphQL 获取链路,以及如何在业务代码中安全地解析和使用它。

一、接口定义与字段说明

该接口定义于 src/responses/insights.service.post.response.ts,完整结构如下:

export interface InsightsServicePostResponseInstagram_actor {
  instagram_actor_id: string;
  id: string;
}
属性名 类型 说明
instagram_actor_id string Instagram 侧使用的"主体(actor)"唯一标识,即该帖子所属账号的 ID,用于标识洞察数据的归属账户。
id string 通用标识符字段。从 SDK 内部 GraphQL 响应反序列化结果看,该值通常与 instagram_actor_id 一致,可视为账号 ID 的另一份冗余表达,便于业务侧按不同命名习惯取值。

从字段命名(instagram_actor,即"Instagram 发布者/行动者")可以推断:该对象描述的是"产生这条内容并拥有其洞察数据的 Instagram 账户实体",而非观看者、评论者等其他参与方。两个字段均非可选、无默认值,属于 Instagram 洞察 GraphQL 接口必然返回的强类型字段。

二、该接口在洞察响应树中的位置

InsightsServicePostResponseInstagram_actor 不是孤立存在的,它是帖子洞察响应中 media 节点的子属性。结合 InsightsServicePostResponseMedia 文档与源码,完整响应层级如下:

InsightsServicePostResponseRootObject
└── data: InsightsServicePostResponseData
    └── media: InsightsServicePostResponseMedia
        ├── id: string
        ├── creation_time: number
        ├── instagram_media_id: string
        ├── instagram_media_owner_id: string
        ├── instagram_actor: InsightsServicePostResponseInstagram_actor  ← 本文主题
        ├── inline_insights_node: InsightsServicePostResponseInline_insights_node
        ├── display_url: string
        ├── image: InsightsServicePostResponseImage
        ├── like_count / comment_count / save_count: number
        └── shopping_outbound_click_count / shopping_product_click_count: number

其中:

  • 顶层入口为 InsightsServicePostResponseRootObject,只含 data 属性;
  • data.media 中的 inline_insights_node 承载核心指标(state、metrics、error);
  • instagram_actor 与 instagram_media_owner_id 同处 media 节点:前者指向账号主体对象,后者是媒体归属账号 ID 的扁平字符串,两者可互为印证。

这一层级全部来自 src/responses/insights.service.post.response.ts 的同名导出接口,并通过 src/responses/index.ts 的 export * 统一汇出,供 SDK 外部直接 import。

三、数据从哪来:InsightsService.post 与底层 GraphQL 请求

要拿到包含 instagram_actor 的洞察数据,入口是 SDK 提供的 InsightsService。核心实现在 src/services/insights.service.ts:

public post(mediaId: string): Promise<InsightsServicePostResponseRootObject> {
  return this.client.ads.graphQL<InsightsServicePostResponseRootObject>({
    surface: {
      name: 'post',
      friendlyName: 'IgInsightsPostInsightsQuery',
    },
    documentId: '2009845309144121',
    variables: {
      query_params: {
        access_token: '',
        id: mediaId,
      },
    },
  });
}

调用方式为 client.insights.post(mediaId),其中 insights 实例由 src/core/client.ts 统一装配(与 ads、account 等仓库同级)。该方法接受帖子的媒体 ID(mediaId),返回 Promise<InsightsServicePostResponseRootObject>。

底层请求经由 AdsRepository 的 graphQL 方法发出(SDK 内部 ads 即为该仓库实例):

public async graphQL<T extends { data: any }>(options: GraphQLRequestOptions): Promise<T> {
  const { body } = await this.client.request.send<T>(
    {
      url: '/api/v1/ads/graphql/',
      method: 'POST',
      qs: {
        locale: this.client.state.language,
        vc_policy: 'insights_policy',
        ...(options.surface.name ? { surface: options.surface.name } : {}),
      },
      form: {
        access_token: options.accessToken,
        fb_api_caller_class: 'RelayModern',
        fb_api_req_friendly_name: options.surface.friendlyName,
        doc_id: options.documentId,
        variables: JSON.stringify(options.variables),
      },
    },
    true,
  );
  return body;
}

关键点:

  • 请求端点:POST /api/v1/ads/graphql/,这是 Instagram 面向广告/内容洞察的 GraphQL 网关;
  • vc_policy 固定为 insights_policy,标识洞察策略上下文;
  • doc_id 为服务端 GraphQL 文档 ID,帖子洞察使用 2009845309144121;
  • fb_api_req_friendly_name 为 IgInsightsPostInsightsQuery(与 src/types/graphql-request.options.ts 中 InsightsFriendlyName 联合类型一致);
  • variables.query_params.id 即传入的 mediaId,access_token 为空字符串;
  • 返回体泛型约束为 { data: any },因此 InsightsServicePostResponseRootObject 顶层必须有 data 字段——这与上文响应树完全吻合。

四、实战:获取并解析 instagram_actor

以下示例展示了完整获取帖子洞察并读取 instagram_actor 的流程(需先完成账号登录与状态持久化,参见 session.example.ts):

import { IgApiClient } from 'instagram-private-api';

async function fetchPostActor() {
  const ig = new IgApiClient();
  ig.state.generateDevice('your_device_id');
  await ig.account.login('your_username', 'your_password');

  // mediaId 为帖子媒体 ID(字符串)
  const insights = await ig.insights.post('1234567890123456789');

  const actor = insights.data.media.instagram_actor;
  console.log('instagram_actor_id:', actor.instagram_actor_id);
  console.log('id:', actor.id);

  // 与媒体归属账号做一致性校验(可选)
  const ownerId = insights.data.media.instagram_media_owner_id;
  if (actor.instagram_actor_id && ownerId && actor.instagram_actor_id !== ownerId) {
    console.warn('actor 与 media owner 不一致,需人工核对');
  }
}

注意事项:

  • 字段兼容性:instagram_actor_id 与 id 均为 string,账号 ID 通常为纯数字字符串,不要用 Number() 强转后再做比较,避免大数精度丢失;
  • 空值处理:SDK 类型将两者声明为必填,但服务端在极少数异常/广告媒体场景下可能出现 null(源码中 ad_media 字段即为 null 字面量类型),生产代码建议先判空再使用;
  • 归属校验:可将 instagram_actor_id 与 media.instagram_media_owner_id 交叉验证,确认洞察数据归属账号与媒体所有者一致;
  • 适用前提:洞察数据通常仅对商业账户(Business/Creator 账户)可用,普通个人账号调用可能无法获得有效指标,甚至返回错误,需结合实际账号类型判断结果有效性。

五、同构接口:Story 洞察中的 instagram_actor

Instagram_actor 结构并非帖子洞察独有。在 src/responses/insights.service.story.response.ts 中,故事(Story)洞察定义了结构完全一致的接口:

export interface InsightsServiceStoryResponseInstagram_actor {
  instagram_actor_id: string;
  id: string;
}

其数据来源同样是 InsightsService 的 story(storyId) 方法与 ads.graphQL 链路(documentId: '2164420446988319'、surface: 'story'、friendlyName: 'IgInsightsStoryInsightsAppQuery')。因此,处理帖子洞察与故事洞察时,可对这两个结构相同的 actor 字段复用同一套解析/校验逻辑。这种"同名同构接口分散在不同响应文件"的命名方式,是 TypeDoc 自动为每个响应接口单独生成文档页的原因——在 docs/modules/responses.md 中可看到完整的接口清单。

六、小结

InsightsServicePostResponseInstagram_actor 虽只有 id 与 instagram_actor_id 两个字段,却是连接"帖子洞察指标"与"账号身份归属"的枢纽:

  1. 它嵌套在 data.media 节点中,与 instagram_media_owner_id 共同标识内容所属账户;
  2. 其数据经 InsightsService.post 通过 AdsRepository.graphQL 的 /api/v1/ads/graphql/ 端点(doc_id: 2009845309144121)获取;
  3. 定义与导出位于 src/responses/insights.service.post.response.ts,与 Story 洞察中的同构接口结构完全一致,可统一处理。

理解该接口及其在响应树中的位置,是正确消费 Instagram 帖子数据洞察、构建内容分析或账号运营报表的第一步。

登录后查看全文
instagram-private-api