我已经在AWS API Gateway中创建了一个Authorizer。该授权者指的是Lambda函数。
我正在使用Postman将标头中的以下值传递给API端点。
{
"type":"TOKEN",
"authorizationToken": "testing2",
"methodArn": "arn:aws:execute-api:us-west-2:444456789012:ymy8tbxw7b/*/GET/"
}
上述标头值在Lambda函数中接收。我可以通过CloudWatch中的日志看到这一点。
我想在标头中传递附加值'clientID'。因此,我在邮递员的标头中传递了以下值。
{
"type":"TOKEN",
"authorizationToken": "testing2",
"methodArn": "arn:aws:execute-api:us-west-2:123456789012:ymy8tbxw7b/*/GET/",
"clientID" : "1000"
}
在这种情况下,Lambda函数不会获取clientID。我检查了SO中的各种线程,并了解可以实现映射标头。所以我做了以下。
在API方法的“方法执行”部分中,我创建了一个新的头clientID。在“集成请求”部分的“ HTTP标头”部分下,我提供了以下值
名称:clientID 映射自:method.request.header.clientID
完成上述操作后,我部署了API并尝试从Postman调用该方法,但是clientID显示为未定义。以下是我在Lambda函数中编写的代码
exports.handler = function(event, context, callback) {
var clientid = event.clientID;
//I always get event.clientID undefined
console.log("The client ID is:" + event.clientID);
}
编辑
以下是CloudWatch Log中的错误。
START RequestId: 274c6574-dea5-4009-b777-a929f84b9a9d Version: $LATEST
2019-09-19T09:40:25.944Z 274c6574-dea5-4009-b777-a929f84b9a9d INFO The client ID is:undefined
2019-09-19T09:40:25.968Z 274c6574-dea5-4009-b777-a929f84b9a9d ERROR Invoke Error
{
"errorType": "Error",
"errorMessage": "Unauthorized",
"stack": [
"Error: Unauthorized",
" at _homogeneousError (/var/runtime/CallbackContext.js:13:12)",
" at postError (/var/runtime/CallbackContext.js:30:51)",
" at callback (/var/runtime/CallbackContext.js:42:7)",
" at /var/runtime/CallbackContext.js:105:16",
" at Runtime.exports.handler (/var/task/index.js:40:4)",
" at Runtime.handleOnce (/var/runtime/Runtime.js:66:25)",
" at process._tickCallback (internal/process/next_tick.js:68:7)"
]
}
答案 0 :(得分:1)
我已经理解了为什么我没有在标题中获取值。我已经完成了以下
1)我在标题中使用了REQUEST类型,而不是TOKEN类型。我通过阅读以下链接了解了这一点。此链接还包含“请求”类型的代码。
https://docs.aws.amazon.com/apigateway/latest/developerguide/apigateway-use-lambda-authorizer.html
2)我从“方法请求”和“集成请求”中删除了所有映射。
3)部署了API。