简体   繁体   English

使用 Amazon API Gateway 时,如何从 Django 后端获取请求中使用的 API 密钥?

[英]When using Amazon API Gateway, how do I get the API key used in the request from a Django backend?

Pretty self explanatory title.相当不言自明的标题。 I'm using API Gateway in AWS, requiring an API key to access a backend written in Django (not using lambda).我在 AWS 中使用 API Gateway,需要一个 API 密钥来访问用 Django 编写的后端(不使用 lambda)。 I need to know how to access the API key used in the request to keep track of who did what at the app level.我需要知道如何访问请求中使用的 API 密钥以跟踪谁在应用程序级别做了什么。

You can use mapping templates and get the API Key from the $context variable, it's the apiKey property inside the identity object: http://docs.aws.amazon.com/apigateway/latest/developerguide/api-gateway-mapping-template-reference.html#context-variable-reference您可以使用映射模板并从 $context 变量中获取 API 密钥,它是身份对象内的 apiKey 属性: http ://docs.aws.amazon.com/apigateway/latest/developerguide/api-gateway-mapping-template -reference.html#context-variable-reference

Create a mapping template for your requests and include the property in it.为您的请求创建一个映射模板并在其中包含该属性。 For example, if you wanted to include the entire request body + the API Key you would do this:例如,如果您想包含整个请求正文 + API 密钥,您可以这样做:

{
  "body": $input.json('$'),
  "apiKey": "$context.identity.apiKey"
} 

Depending on how your backend application is built, you could send the API key to your application in a HTTP parameter (path, query string, or header) or in the request body.根据您的后端应用程序的构建方式,您可以在 HTTP 参数(路径、查询字符串或标头)或请求正文中将 API 密钥发送到您的应用程序。 Please have a read through the docs on how to move data between the two systems.请通读有关如何在两个系统之间移动数据的文档

Thanks, Ryan谢谢,瑞安

Here is how I finally made it work.这是我最终使它工作的方式。 At the top or bottom of the template, include this line.在模板的顶部或底部,包含此行。

#set($context.requestOverride.header.x-api-key = $context.identity.apiKey)

When your backend receives this request, the api key will be in the header x-api-key .当您的后端收到此请求时,api 密钥将位于标头x-api-key

Here is a basic mapping template that just forwards the (json) body and the header.这是一个基本的映射模板,它只转发 (json) 正文和标题。

$input.json("$")
#set($context.requestOverride.header.x-api-key = $context.identity.apiKey)

API Gateway uses the X-API-Key header, so I like for my backend to also use that. API Gateway 使用 X-API-Key 标头,所以我希望我的后端也使用它。 That way I can use the same testing commands with only the URL being different.这样我就可以使用相同的测试命令,只是 URL 不同。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 当我运行开发服务器并收到API的GET请求时,从Django的哪儿开始? - Where do I start with Django when I have a development server running and have a GET request from API? 如何在 Django API GET 请求中与 websocket 通信? - How do I communicate with a websocket within a Django API GET request? 在Django后端上的API密钥实现 - API Key implementation on a Django backend 如何根据发出请求的用户动态过滤 django rest api get request 中返回的字段? - How do I dynamically filter fields returned in django rest api get request based on the user making the request? Not able to make a GET request to the django REST API backend for the id=1 article, from axios in reactjs - Not able to make a GET request to the django REST API backend for the id=1 article, from axios in reactjs 如何获取 API 密钥消息以消除 TinyMCE? (姜戈) - How do I get the API Key message to go away TinyMCE? (Django) 如何在Django中构造用于发送api get请求的视图? - How do I structure my view for sending an api get request in Django? 如何在Django函数中发出REST REST请求的POST请求? - How do I make a POST request from within a django function to rest api? 当我插入外部 api 进行项目时,我如何解决 Django 不接受任何参数错误 - How do I resolve Django takes no arguments error I get when I plug in external api to project 如何从Django rest API中的ModelViewSet类获取请求的用户名? - How to get username of request from ModelViewSet class in Django rest API?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM