Vì bạn muốn gọi API thông qua chứng thực danh tính Cognito, đầu tiên
- Sửa đổi vai trò auth của identitypool có api thực hiện chính sách, bạn chỉ có thể đính kèm các chính sách quản lý "AmazonAPIGatewayInvokeFullAccess" đến vai trò tương ứng
- Trong API gateway theo yêu cầu phương pháp tương ứng, thêm Authorization như "AWS_IAM"
Bạn cần đăng nhập theo yêu cầu khi sử dụng "IAM" auth, giải thích đây https://docs.aws.amazon.com/general/latest/gr/signing_aws_api_requests.html
Thay vì # 3, bạn có thể tạo và tải xuống SDK từ bảng điều khiển giai đoạn của cổng API và thực hiện cuộc gọi đến api qua sdk.
Một khi bạn có được phiên cognito, bạn có thể thực hiện cuộc gọi bằng cách sử dụng sdk như dưới đây
var apigClient = apigClientFactory.newClient({
accessKey: AWSCognito.config.credentials.accessKeyId,
secretKey: AWSCognito.config.credentials.secretAccessKey,
sessionToken: AWSCognito.config.credentials.sessionToken
});
var params = {
// This is where any modeled request parameters should be added.
// The key is the parameter name, as it is defined in the API in API Gateway.
};
var body = {};
var additionalParams = {
// If there are any unmodeled query parameters or headers that must be
// sent with the request, add them here.
headers: {
'Content-Type': 'application/json'
},
queryParams: {}
};
apigClient.<resource><Method>(params, body, additionalParams)
.then(function(result) {
//
}).catch(function(err) {
//
});
Thanks a lot Partha! Tôi sẽ thử cái này – Mazzaroth