Instagram嵌入API获取的HTML无法在WebView中很好地显示


您可以使用以下API获得用于嵌入Instagram帖子的HTML。

  • https://api.instagram.com/oembed?url= {发布网址}

响应以JSON返回,并且html键包含用于嵌入式显示的HTML。

1
2
3
4
5
6
{
    "author_id": 123456,
    "author_name": "eimei4coding",
    /* ?その他諸々? */
    "html":"...", // <-- ここに埋め込み表示用のHTMLが入っている
}

官方API文档

  • https://www.instagram.com/developer/embedding/

当我使用

在WebView中显示此HTML时,未嵌入并显示默认图像(仅具有灰色背景的Instagram图标)?

如果查看

html的内容,由于某种原因,加载embed.js的部分(src)中缺少协议(https或http)???

1
<script async defer _src="//www.instagram.com/embed.js"></script>

没问题,因为该协议用于在网页中嵌入和显示,但是它是一条打孔线,在其他情况下不会显示。例如,我的案例不起作用,因为我尝试使用WKWebView#loadHTMLString显示它。

因此,我替换了它并添加了https:来解决它。

1
<script async defer _src="https://www.instagram.com/embed.js"></script>