我在AWS中有一个API网关,该网关启用了IAM授权。它与邮递员一起正常工作,但不适用于localhost反应代码。这是我的代码
var AWSSignature = require('react-native-aws-signature');
var query = this.query.current.value;
var body = {'id': Auth.user.username,
'message': query,
'timestamp': new Date()}
var awsSignature = new AWSSignature();
let credentials = {
SecretKey: '***',
AccessKeyId: '**'
};
var d = new Date().toISOString().split('.')[0]+"Z";
d=d.split('-').join('');
d=d.split(':').join('');;
console.log(d);
var options = {
path: '/v1/chatbot',
service: 'execute-api',
method: 'POST',
headers: {
'X-Amz-Date': d,
'host': '<api>.us-east-2.amazonaws.com'
},
region: 'us-east-2',
body: JSON.stringify(body),
credentials
};
awsSignature.setParams(options);
var signature = awsSignature.getSignature();
var authorization = awsSignature.getAuthorizationHeader();
console.log(authorization);
var headers = {
'Content-Type': 'application/x-www-form-urlencoded',
'Authorization': authorization
}
axios.post('<api>/v1/chatbot', JSON.stringify(body), {headers: headers})
.then((response) => {
console.log("success");
console.log(response);
})
.catch((error) => {
console.log(error);
})
我实际上是在关注此链接: https://www.npmjs.com/package/react-native-aws-signature 但它似乎不起作用 我仍然缺少身份验证令牌! 我也尝试添加允许跨域标头,但这也不起作用