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

如何通过LinkedIn Posts API获取帖子中的图片URL?

解决LinkedIn Posts API单次获取帖子与图片URL的问题

要在单次API调用中同时拿到帖子信息和图片URL,你需要调整请求的fields参数,明确指定媒体对象的嵌套字段,展开包含图片URL的具体数据。以下是具体方案:

核心调整:完善fields参数

LinkedIn Posts API需要明确指定媒体类型的子字段,尤其是图片对应的VectorImage对象,通过~装饰器展开其包含URL的artifacts数组。修改后的fields参数应为:

id,content:(media:(description,media:(com.linkedin.common.VectorImage~:*(artifacts))))

完整请求代码示例

let testresp = await axios.get(`https://api.linkedin.com/rest/posts?q=author&author=urn%3Ali%3Aorganization%3A${LinkedInHelpers.URNtoID(orgsRes.data.elements[0].organizationalTarget)}&fields=id,content:(media:(description,media:(com.linkedin.common.VectorImage~:*(artifacts))))`, {
    headers: {
        'Authorization': `Bearer ${accessToken.data.access_token}`,
        'X-Restli-Protocol-Version': '2.0.0',
        'LinkedIn-Version': '202301'
    }
})

提取图片URL

请求成功后,返回数据的content.media.elements数组中,每个媒体对象会包含com.linkedin.common.VectorImage字段,其artifacts数组里的url就是不同尺寸的图片地址。示例提取逻辑:

testresp.data.elements.forEach(post => {
    if (post.content?.media?.elements) {
        post.content.media.elements.forEach(mediaItem => {
            const vectorImage = mediaItem.media['com.linkedin.common.VectorImage'];
            if (vectorImage?.artifacts) {
                // 可按需选择不同尺寸,这里取最大尺寸的URL
                const imageUrl = vectorImage.artifacts.at(-1).url;
                console.log('图片URL:', imageUrl);
            }
        });
    }
});

注意事项

  • 确保你的应用拥有r_organization_social权限,这是访问企业帖子媒体数据的必要权限。
  • 你指定的LinkedIn-Version: 202301完全兼容上述字段嵌套查询规则。

内容的提问来源于stack exchange,提问作者Juan Arturo Blanco Nogueira

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.03 07:21:23