清单 11 中的控制台会话显示了运行清单 10 中脚本得到的响应示例。
清单 11. 使用简单的 Google 目录服务 Ruby 脚本
$ ./gdir.rb adexchangebuyer Ad Exchange Buyer API ./apis/adexchangebuyer/v1.1/rest adsense AdSense Management API ./apis/adsense/v1.1/rest adsensehost AdSense Host API ./apis/adsensehost/v4.1/rest analytics Google Analytics API ./apis/analytics/v3/rest androidpublisher Google Play Android Developer API ./apis/androidpublisher/v1/rest audit Enterprise Audit API ./apis/audit/v1/rest bigquery BigQuery API ./apis/bigquery/v2/rest blogger Blogger API ./apis/blogger/v3/rest books Books API ./apis/books/v1/rest calendar Calendar API ./apis/calendar/v3/rest compute Compute Engine API ./apis/compute/v1beta12/rest coordinate Google Maps Coordinate API ./apis/coordinate/v1/rest customsearch CustomSearch API ./apis/customsearch/v1/rest dfareporting DFA Reporting API ./apis/dfareporting/v1/rest discovery APIs Discovery Service ./apis/discovery/v1/rest drive Drive API ./apis/drive/v2/rest ... storage Cloud Storage API ./apis/storage/v1beta1/rest taskqueue TaskQueue API ./apis/taskqueue/v1beta2/rest tasks Tasks API ./apis/tasks/v1/rest translate Translate API ./apis/translate/v2/rest urlshortener URL Shortener API ./apis/urlshortener/v1/rest webfonts Google Web Fonts Developer API ./apis/webfonts/v1/rest youtube YouTube API ./apis/youtube/v3alpha/rest youtubeAnalytics YouTube Analytics API ./apis/youtubeAnalytics/v1/rest $
清单 11 中的输出显示了 API 名称、它们的标题,以及进一步分析每个 API 的 URL 路径。
结束语
本文中的示例演示了公共 API 在从 Internet 提取信息方面的强大功能。与 Web 抓取和爬取 (spidering) 相比,Web API 提供了访问有针对性的特定信息的能力。Internet 上在不断创造新价值,这不仅通过使用这些 API 来实现,还通过用新颖的方式组合它们,从而向越来越多的 Web 用户提供新数据来实现。
但是请记住,使用 API 需要付出一定的代价。限制问题就常让人抱怨。同样,可能在不通知您的情况下更改 API 规则这一事实,因此在构建应用程序时必须加以考虑。最近,Twitter 更改了它的 API 来提供 “一种更加一致的体验”。这一更改对许多可能被视为典型 Twitter Web 客户端竞争对手的第三方应用程序而言,无疑是一场灾难。










