Ans*_*Cen 6 amazon-cognito aws-sdk aws-lambda react-native aws-amplify
我创建了一个AWS移动中心项目,包括Cognito和Cloud逻辑.在我的API网关中,我为授权者设置了Cognito用户池.我使用React native作为我的客户端应用程序.如何将Authorization标头添加到我的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);
});
};
Run Code Online (Sandbox Code Playgroud)
Mik*_*ick 11
默认情况下,API模块aws-amplify
将尝试sig4签名请求.如果您的授权人类型是,那就太棒了AWS_IAM
.
使用Cognito用户池授权程序时,这显然不是您想要的.在这种情况下,您需要在Authorization
标头中传递id_token ,而不是sig4签名.
今天,你确实可以传递一个Authorization
标题来放大,它将不再用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);
}
Run Code Online (Sandbox Code Playgroud)
使用aws-amplify
0.4.1进行测试.
归档时间: |
|
查看次数: |
3832 次 |
最近记录: |