Adding custom authorization gives error : The request signature we calculated does not match the signature you provided

Adding custom authorization gives error : The request signature we calculated does not match the signature you provided

我有时(不总是)遇到这个错误。我在 API 网关函数中创建了一个资源和一个方法。然后我将它映射到 Lambda 函数。在那里自己测试,一切正常。

现在我在方法中添加了自定义授权功能。现在,如果我在那里测试它,

The request signature we calculated does not match the signature you provided. Check your AWS Secret Access Key and signing method. Consult the service documentation for details.\n\nThe Canonical String for this request should have been\n'POST\n/2015-03-31/functions/arn%3Aaws%3Alambda%3Aus-eas

更多细节:属于该方法的lambda函数已被删除。然后我re-created同名函数。并注意到部署 API 时删除了该方法。所以我 re-created 映射并将其映射到 Lambda 函数。从那以后,我就面临这个问题。我确信如果我更改 Lambda 的名称,它可能会解决问题。但我认为这是 AWS 的一个错误,而不是我的问题。需要澄清我是否做错了什么?

看起来 API 当前部署的版本正在将授权 header 从方法请求转发到集成请求 (Lambda)。如果您正在尝试修复 API 的当前状态,那么我建议从方法请求中删除授权 header,这也会将其从集成请求中删除。这应该可以修复 Lambda 签名错误。

如果您尝试在方法上使用自定义授权方,则无需在方法请求中设置授权 header。您只需要将授权方标识源设置为 'method.request.header.Authorization'