使用oEmbed API获取HTML来嵌入Twitter微博


Twitter推文可以从各个推文中手动获取嵌入代码,但是您可以使用API??从推文URL中获取嵌入格式。

使用Twitter的oEmbed API。链接目标列出了示例响应和可以附加的各种查询参数。

这次,我用ruby包裹了它。

1
2
3
4
5
6
7
8
9
10
$ irb -rpp
irb(main):001:0> require 'oembed'
=> true
irb(main):002:0> OEmbed::Providers.register(OEmbed::Providers::Twitter)
=> [#<OEmbed::Provider:0x000055e464082ef8 @endpoint="https://publish.twitter.com/oembed", @urls=[/^https:\/\/([^\.]+\.)?twitter\.com\/(.*?)\/status\/(.*?)/], @format=:json>]
irb(main):003:0> OEmbed::Providers::Twitter.endpoint += "?=omit_script=true"
irb(main):004:0> resource = OEmbed::Providers.get("https://twitter.com/aximovich/status/125
1416407582994435")
irb(main):005:0> resource
=>JSONがオブジェクトになったもの

通过将参数

omit_script设置为true,这次我们省略了用于加载嵌入脚本的标记。

返回的json(已成为对象)如下。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
irb(main):006:0> resource.fields
=>
{
  "url"=>"https://twitter.com/aximovich/status/1251416407582994435",
  "author_name"=>"Aximov",
  "author_url"=>"https://twitter.com/aximovich",
  "html"=>"<blockquote class="twitter-tweet"><p lang="und" dir="ltr"><a href="https://twitter.com/hashtag/%E3%81%A9%E3%81%86%E3%81%B6%E3%81%A4%E3%81%AE%E6%A3%AE?src=hash&amp;ref_src=twsrc%5Etfw">#どうぶつの森</a> <a href="https://twitter.com/hashtag/AnimalCrossing?src=hash&amp;ref_src=twsrc%5Etfw">#AnimalCrossing</a> <a href="https://twitter.com/hashtag/ACNH?src=hash&amp;ref_src=twsrc%5Etfw">#ACNH</a> <a href="https://twitter.com/hashtag/NintendoSwitch?src=hash&amp;ref_src=twsrc%5Etfw">#NintendoSwitch</a> <a href="https://t.co/PuDKkaGIMr">pic.twitter.com/PuDKkaGIMr</a></p>&mdash; Aximov (@aximovich) <a href="https://twitter.com/aximovich/status/1251416407582994435?ref_src=twsrc%5Etfw">April 18, 2020</a></blockquote>\n",
  "width"=>550,
  "height"=>nil,
  "type"=>"rich",
  "cache_age"=>"3153600000",
  "provider_name"=>"Twitter",
  "provider_url"=>"https://twitter.com",
  "version"=>"1.0"
}

在上面的示例中,您可以使用resource.html来获取实际嵌入的HTML。

1
2
irb(main):007:0> resource.html
=> "<blockquote class="twitter-tweet"><p lang="und" dir="ltr"><a href="https://twitter.com/hashtag/%E3%81%A9%E3%81%86%E3%81%B6%E3%81%A4%E3%81%AE%E6%A3%AE?src=hash&amp;ref_src=twsrc%5Etfw">#どうぶつの森</a> <a href="https://twitter.com/hashtag/AnimalCrossing?src=hash&amp;ref_src=twsrc%5Etfw">#AnimalCrossing</a> <a href="https://twitter.com/hashtag/ACNH?src=hash&amp;ref_src=twsrc%5Etfw">#ACNH</a> <a href="https://twitter.com/hashtag/NintendoSwitch?src=hash&amp;ref_src=twsrc%5Etfw">#NintendoSwitch</a> <a href="https://t.co/PuDKkaGIMr">pic.twitter.com/PuDKkaGIMr</a></p>&mdash; Aximov (@aximovich) <a href="https://twitter.com/aximovich/status/1251416407582994435?ref_src=twsrc%5Etfw">April 18, 2020</a></blockquote>\n"

单击此处进行嵌入的必要准备。

这次,我选择了omit_script=true,但是您可以直接嵌入没有此代码的HTML。 (但是,它将是HTML,每个推文都具有所有嵌入脚本)

请注意,检索到的字符串已转义,因此您需要在使用前将其还原为HTML。例如,对于Rails,建议使用sanitize。 (但是,请注意sanitize会删除script标记。)对于Node.js,有很多解码URI(例如,decodeURI)或其他解码包(例如html-entities)。

这次我在Ruby中获得了它,但是如果您点击了API,则可以使用curl,因此语言完全无关紧要。另外,Twitter的oEmbed API不需要速率限制或API密钥,因此您可以立即试用。有关详细信息,请参见参考。