我创建了一个 AWS 移动中心项目,包括 Cognito 和云逻辑。在我的 API 网关中,我为授权者设置了 Cognito 用户池。我使用 React Native 作为我的客户端应用程序。如何将授权标头添加到我的 API 请求中。
const request = {
body: {
attr: value
}
};
API.post(apiName, path, request)
.then(response => {
// Add your code here
console.log(response);
})
.catch(error => {
console.log(error);
});
};
默认情况下,API 模块aws-amplify
将尝试对请求进行 sig4 签名。如果您的授权人类型是AWS_IAM
.
这显然是not使用 Cognito 用户池授权程序时您想要什么。在这种情况下,您需要在Authorization
标头,而不是 sig4 签名。
今天,您确实可以通过Authorization
要放大的标头,以及它不会再用 sig4 签名覆盖它 https://github.com/aws/aws-amplify/issues/37.
在你的情况下,你只需要添加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.
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)