默认情况下,API 模块aws-amplify
会尝试对请求进行 sig4 签名。如果您的 Authorizer 类型是 ,那就太好了AWS_IAM
。
这显然不是您在使用 Cognito 用户池授权器时想要的。在这种情况下,您需要在Authorization
标头中传递 id_token,而不是 sig4 签名。
今天确实可以传一个Authorization
header来放大,不会再用sig4签名覆盖了。
在您的情况下,您只需要将对象添加headers
到您的request
对象中。例如:
async function callApi() {
// You may have saved off the JWT somewhere when the user logged in.
// If not, get the token from aws-amplify:
const user = await Auth.currentAuthenticatedUser();
const token = user.signInUserSession.idToken.jwtToken;
const request = {
body: {
attr: "value"
},
headers: {
Authorization: token
}
};
var response = await API.post(apiName, path, request)
.catch(error => {
console.log(error);
});
document.getElementById('output-container').innerHTML = JSON.stringify(response);
}
使用aws-amplify
0.4.1 测试。