aws-lambda - AWS API Gateway:如何为我的用户生成 apiKey 并让他们将其包含在查询参数中?
问题描述
目标:我们只想让我们的客户使用我们的 HTTP 端点,首先生成一个 APIkey,然后将其传递到请求 URI 参数中以触发 lambda 函数。
我尝试了什么:不确定如何以编程方式生成 apiKey。决定用我在 API Gateway 中找到的 apiKey 进行测试,并在 Lambda 函数中编写了以下代码:
exports.handler = async (event, context, callback) => {
const apikey =
event.queryStringParameters.apikey || event.stageVariables.apikey;
const tmp = event.methodArn.split(":");
const apiGatewayArnTmp = tmp[5].split("/");
const awsAccountId = tmp[4];
const region = tmp[3];
const restApiId = apiGatewayArnTmp[0];
const stage = apiGatewayArnTmp[1];
callback(null, {
principalId: "principalId",
policyDocument: {
Version: "2012-10-17",
Statement: [
{
Action: "execute-api:Invoke",
Effect: "Allow",
Resource: `arn:aws:execute-api:${region}:${awsAccountId}:${restApiId}/${stage}/*/*`
}
]
},
usageIdentifierKey: apikey
});
return context.succeed('Success!');
};
在 API Gateway 中,我还添加了一个带有 request querystring 的自定义授权者apikey
。
当我单击测试并为键查询字符串传递任何字符串值时,apikey
我得到 200OK 并显示以下内容:
Response
Response Code: 200
Latency 2607
Policy
{
"Version": "2012-10-17",
"Statement": [
{
"Action": "execute-api:Invoke",
"Effect": "Allow",
"Resource": "arn:aws:execute-api:us-west-2:2234235234:asdf3r3r/ESTestInvoke-stage/*/*"
}
]
}
当我打开时,https://s9fs7h0.execute-api.us-west-2.amazonaws.com/default/executeWSL?apikey=myAPIkeygoeshere
我仍然得到{"message":"Forbidden"}
我哪里做错了?我预计导航该 URL 会返回Success!
解决方案
推荐阅读
- python - 所以我正在尝试使用 atom 在我的 mac 上运行 python 脚本,但由于某种原因它无法正常工作
- javascript - 电子邮件功能在本地服务器上有效,但在 Web 服务器上无效
- javascript - Javascript中的原型概念,我说对了吗?
- spring-data-jpa - DB2 不接受 UUID
- reactjs - 如何在路由更改时获取 React 路由器路径
- android - 使用两个 RecyclerView 滚动页面(片段):最先进的技术以及如何回收?
- r - R,数据框操作,排序
- php - 如何使用 PHP 将字节流发送到套接字?
- javascript - 三元不更新 React 中的视图
- python - 这个冒泡排序逻辑是否正确