API网关第三弹:kong里面添加bearer token的服务

344 阅读5分钟
在第二弹里面,把天气服务成功添加到kong里面,它用的是apikey的方式
再另一个服务中,可能就不一样

网站介绍

今天给大家介绍一个新的好网站,电影集合的信息网站。 www.themoviedb.org/

API获取和使用

网站支持通过API的访问,获取API的网址

www.themoviedb.org/settings/ap…

收费情况

免费

API指令

一共连同账号管理等加起来,一百多个指令。给大家一点小小的震撼,看下图。

截不完,真的截图不完,太长了。

image.png

我们只选取其中最适合的search里面的movie选项。

最常用指令为例

网站提供了非常直观且便捷的调试页面,网页调试可以用这个链接。

developer.themoviedb.org/reference/s…

查询下盗梦空间(英文名inception)

curl --request GET \
     --url 'https://api.themoviedb.org/3/search/movie?query=inception&include_adult=false&language=en-US&page=1' \
     --header 'Authorization: Bearer v50key' \
     --header 'accept: application/json'

其中v50key,你要换成你自己的

查询返回的结果,是一个json格式的数组

{
  "page": 1,
  "results": [
    {
      "adult": false,
      "backdrop_path": "/28kKbSUvUz6P5RE1AuMJMO7IMfK.jpg",
      "genre_ids": [
        28,
        878,
        12
      ],
      "id": 27205,
      "original_language": "en",
      "original_title": "Inception",
      "overview": "Cobb, a skilled thief who commits corporate espionage by infiltrating the subconscious of his targets is offered a chance to regain his old life as payment for a task considered to be impossible: "inception", the implantation of another person's idea into a target's subconscious.",
      "popularity": 343.415,
      "poster_path": "/oYuLEt3zVCKq57qu2F8dT7NIa6f.jpg",
      "release_date": "2010-07-15",
      "title": "Inception",
      "video": false,
      "vote_average": 8.367,
      "vote_count": 35739
    },
    {
      "adult": false,
      "backdrop_path": "/JeGkRdNsOuMrgwBdtB0hp763MU.jpg",
      "genre_ids": [
        18,
        53
      ],
      "id": 613092,
      "original_language": "es",
      "original_title": "El crack cero",
      "overview": "Madrid, Spain, 1975; shortly after the end of the Franco dictatorship. Six months after the mysterious death of his lover, a prestigious tailor, a married woman visits the office of the young Germán Areta, a former police officer turned private detective, to request his professional services.",
      "popularity": 7.383,
      "poster_path": "/kzgPu2CMxBr4YZZxC1Off4cUfR9.jpg",
      "release_date": "2019-10-04",
      "title": "The Crack: Inception",
      "video": false,
      "vote_average": 6.6,
      "vote_count": 37
    },
    {
      "adult": false,
      "backdrop_path": null,
      "genre_ids": [
        16,
        28,
        53,
        878
      ],
      "id": 64956,
      "original_language": "en",
      "original_title": "Inception: The Cobol Job",
      "overview": "This "Inception" prequel unfolds courtesy of a beautiful Motion Comic, and explains how Cobb, Arthur and Nash were enlisted by Cobol Engineering.",
      "popularity": 12.422,
      "poster_path": "/sNxqwtyHMNQwKWoFYDqcYTui5Ok.jpg",
      "release_date": "2010-12-07",
      "title": "Inception: The Cobol Job",
      "video": false,
      "vote_average": 7.262,
      "vote_count": 300
    },
    {
      "adult": false,
      "backdrop_path": null,
      "genre_ids": [],
      "id": 350632,
      "original_language": "en",
      "original_title": "Nātyārambham",
      "overview": "On certain Adavus: the basic steps in Bharatanatyam. The first part covers Tatta Adavu, Natta Adavu, Visharu Adavu , Tatti Metti Adavu and Teermanam Adavu.",
      "popularity": 0.404,
      "poster_path": null,
      "release_date": "",
      "title": "The Inception of Dramatic Representation",
      "video": true,
      "vote_average": 5.8,
      "vote_count": 3
    },
    {
      "adult": false,
      "backdrop_path": null,
      "genre_ids": [
        35
      ],
      "id": 542438,
      "original_language": "en",
      "original_title": "Bikini Inception",
      "overview": "Two flunky Janitors in an Arctic Lab perform unauthorized experiments transporting them to a beach dream world in Malibu California w/50 beautiful young girls and a female Brazilian PhD Student wearing only a bra and panties. A '67 Muscle car races chases horses guns fights surfing, sumo wrestler, wolf monster, underwater scenes tons of gorgeous models. Sexy sci-fi fun.",
      "popularity": 10.484,
      "poster_path": "/mNASlEOFX2c9upxaSbgeKFvIr1L.jpg",
      "release_date": "2015-05-19",
      "title": "Bikini Inception",
      "video": false,
      "vote_average": 7,
      "vote_count": 1
    },
    {
      "adult": false,
      "backdrop_path": null,
      "genre_ids": [
        10402
      ],
      "id": 973484,
      "original_language": "en",
      "original_title": "Inception: Music from the Motion Picture",
      "overview": "The Special Features Blu-ray disc contains a lossless DTS-HD MA 5.1 surround mix of the Inception soundtrack.",
      "popularity": 1.221,
      "poster_path": "/7uM4DyRVAcgagvhZoWrkrqMPbqV.jpg",
      "release_date": "2010-12-07",
      "title": "Inception: Music from the Motion Picture",
      "video": true,
      "vote_average": 6,
      "vote_count": 1
    },
    {
      "adult": false,
      "backdrop_path": null,
      "genre_ids": [],
      "id": 250845,
      "original_language": "en",
      "original_title": "WWA The Inception",
      "overview": "The first World Wrestling Allstars pay per view, live from Sydney, Australia! A tournament titled "7 Deadly Sins", each round having a stipulation match, the winner will be crowned the first ever WWA Heavyweight Champion! Wrestlers such as Jeff Jarrett, Road Dogg, Jerry Lawler all compete in the tournament, with the WWA Commissioner, Bret Hart not too far away to make sure nothing gets to far out of hand!",
      "popularity": 3.275,
      "poster_path": null,
      "release_date": "2001-10-26",
      "title": "WWA The Inception",
      "video": true,
      "vote_average": 3.1,
      "vote_count": 5
    }
  ],
  "total_pages": 1,
  "total_results": 7
}

添加TMDB的服务到kong里面

敲重点,在第四步和上次的不一样

示例的curl指令如下

curl --request GET \
     --url 'https://api.themoviedb.org/3/search/movie?query=inception&include_adult=false&language=en-US&page=1' \
     --header 'Authorization: Bearer v50key' \
     --header 'accept: application/json'

其中query中的inception是我想要去搜寻的关键词,作为参数输入进来。其他几个参数include_adult, language, page等先选择默认的参数输入。

下面是如何将你的curl指令添加到Kong网关中的详细步骤:

1. 创建服务(Service)

首先,在Kong中创建一个服务,这个服务将代理你的电影数据库API。

curl -i -X POST \
  --url http://localhost:8001/services/ \
  --data "name=movie_service" \
  --data "url=https://api.themoviedb.org/3/search/movie"

这里,name 是你为服务定义的名称(例如 movie_service),url 是后端服务的URL(不包括查询参数)。

2. 创建路由(Route)

接下来,为 movie_service 创建一个路由,定义客户端请求的路径。

curl -i -X POST \
  --url http://localhost:8001/services/movie_service/routes \
  --data "paths[]=/movies"

这里,paths 参数定义了客户端可以访问的路径,/movies 表示所有以 /movies 开头的请求都将被转发到 movie_service

3. 创建消费者(Consumer)

在Kong中,每个通过API进行的请求都必须与一个消费者(Consumer)关联。消费者可以是个人或第三方应用程序。

curl -i -X POST \
  --url http://localhost:8001/consumers/ \
  --data "username=movie_app_user"

这里,username 是你为消费者定义的名称(例如 movie_app_user)。

4. 使用正确的认证方式 bearer token

这个案例中不是用key-auth 插件

你需要确保 Kong 使用的是 Bearer Token 而不是 key-auth 插件。可以通过配置 Kong 的 request-transformer 插件来添加Bearer Token到请求头中。

现在,为 movie_service 添加 request-transformer 插件,将Bearer Token添加到请求头中:

sh
Copy code
curl -i -X POST \
  --url http://localhost:8001/services/movie_service/plugins/ \
  --data "name=request-transformer" \
  --data "config.add.headers=Authorization: Bearer v50key"

其中把v50key换成真实的key

5. 测试服务

完成上述步骤后,你可以通过Kong的代理端口(默认是8000)来测试你的服务。

curl -i -X GET "http://localhost:8000/movies?query=inception&include_adult=false&language=en-US&page=1"

这次就不每一步都截图了,最终截一个调用成功的如下:

image.png