gpt4 book ai didi

react-native - AWS-amplify 在请求中包含 cognito Authorization header

转载 作者:行者123 更新时间:2023-12-04 05:11:56 25 4
gpt4 key购买 nike

我创建了一个包含 Cognito 和云逻辑的 AWS 移动中心项目。在我的 API 网关中,我为授权者设置了 Cognito 用户池。我使用 React Native 作为我的客户端应用程序。如何将授权 header 添加到我的 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);
});
};

最佳答案

默认情况下,aws-amplify 的 API 模块将尝试对请求进行 sig4 签名。如果您的授权人类型是 AWS_IAM,那就太好了。 .

这显然不是您在使用 Cognito 用户池授权器时想要的。在这种情况下,您需要在 Authorization 中传递 id_token header ,而不是 sig4 签名。

今天,你确实可以通过 Authorization header 放大,it will no longer overwrite it with the sig4 signature .

在您的情况下,您只需添加 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。

关于react-native - AWS-amplify 在请求中包含 cognito Authorization header ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50645158/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com