在 AWS 中,如何将 API 键添加到我的 API?

In AWS how do I add an API key to my API?

您好:我一直在学习 AWS 文档中的两个教程:

创建示例宠物店API (https://docs.aws.amazon.com/apigateway/latest/developerguide/api-gateway-create-api-from-example.html)

...并创建一个 API 密钥 (https://docs.aws.amazon.com/apigateway/latest/developerguide/api-gateway-setup-api-key-with-console.html#api-gateway-usage-plan-create-apikey)。

当我使用提供的 URL 测试宠物商店的获取方法时,它 returns:

Welcome to your Pet Store API You have successfully deployed your first API. You are seeing this HTML page because the GET method to the root resource of your API returns this content as a Mock integration.

The Pet Store API contains the /pets and /pets/{petId} resources. By making a GET request to /pets you can retrieve a list of Pets in your API. If you are looking for a specific pet, for example the pet with ID 1, you can make a GET request to /pets/1.

You can use a REST client such as Postman to test the POST methods in your API to create a new pet. Use the sample body below to send the POST request:

{ "type" : "cat", "price" : 123.11 }

现在我转到 API 网关 -> API -> 资源 -> -> 方法请求 -> API Key Required 并将其更改为“True”并重新部署.

当我转到提供的URL进行测试时,现在页面returns:

{"message":"Forbidden"}

这有道理...我告诉它 API required = true, right?

所以我的问题是,如何传递 API 密钥?这样我就不会得到“禁止”的结果?我在上面粘贴的教程链接中没有看到这一点,也无法在其他地方找到。

假设您已完成创建 API 密钥的所有步骤,您可以通过在请求中的 x-api-key header 中指定它来使用此 API 密钥。

You distribute API keys to your customers and require them to pass the API key as the X-API-Key header of each incoming request.

有关在 API 网关中使用 API 密钥的更多信息,请访问:Choose an API key source - Amazon API Gateway

  1. 你创建了一个Usage Plans
  2. 将此使用计划附加到您的 API 和 Stage
  3. 创建 API Key
  4. 现在用名为 x-api-key 的 header 调用你的 API 并且它的值是在步骤 3
  5. 中创建的 API Key

样本: curl -i -H "x-api-key: Cd2YiWs8Fv8Lg6njI0wXf1iiNOE94XjM3EQe8567" -X GET https://7r9cvghbf4.execute-api.ap-northeast-2.amazonaws.com/dd/pets