正如@Sam 提到的,您需要生成一个签名并将其与您的http 请求一起发送。您可以通过两种方式做到这一点。
- 您可以生成签名并将签名与您的 http 请求一起作为标头发送
- 您可以使用可以生成签名并发送请求的库
方法一——生成签名
### generate signature
const aws4 = require('aws4')
const signature = aws4.sign({
host: 'https://apiId.execute-api.ap-southeast-2.amzonaws.com',
method: 'GET',
path: '/development/hello',
headers: {
},
region: 'ap-southeast-2',
service: 'execute-api'
}, {
secretAccessKey: "your access key",
accessKeyId: "your secret key",
sessionToken: "your session token if you are using temporary credentials"
})
// output
{ "host":
"something.execute-api.ap-southeast-2.amzonaws.com",
"method": "GET",
"path": "/development/hello",
"headers": {
"Host":
"something.execute-api.ap-southeast-2.amzonaws.com",
"X-Amz-Security-Token":
"security token",
"Authorization":
"AWS4-HMAC-SHA256 Credential=ASIARNZFFFFFEGFG23JY/20191212/ap-southeast-2/execute-api/aws4_request, SignedHeaders=host;x-amz-date;x-amz-security-token;x-apigw-api-id, Signature=7fd8e51c2bf4faefaRRRRRf92c700799b78234d204"
},
"region": "ap-southeast-2",
"service": "execute-api"
}
将Authorization、X-Amz-Date 和X-Amz-Security-Token 作为您的http 请求的标头。
方法二——使用可以生成签名并发送请求的库
var apigClientFactory = require('aws-api-gateway-client').default;
var apigClient = apigClientFactory.newClient({
invokeUrl:'https://apiId.execute-api.ap-southeast-2.amzonaws.com/development', // REQUIRED
accessKey: 'your access key', // REQUIRED
secretKey: 'your secret key', // REQUIRED
sessionToken: 'your session token if you are using temporary credentials',
region: 'ap-southeast-2', // REQUIRED: The region where the API is deployed.
systemClockOffset: 0, // OPTIONAL: An offset value in milliseconds to apply to signing time
retries: 4, // OPTIONAL: Number of times to retry before failing. Uses axon-retry plugin.
retryCondition: (err) => { // OPTIONAL: Callback to further control if request should be retried. Uses axon-retry plugin.
return err.response && err.response.status === 500;
}
});
(() => {
apigClient.invokeApi(null, `/hello`, 'GET', {
headers: {
}
})
.then(function(result){
console.log('result: ', result)
//This is where you would put a success callback
}).catch( function(result){
console.log('result: ', result)
//This is where you would put an error callback
});
})()
希望对你有帮助,祝你好运