我正在尝试使用aws4软件包使用Javascript调用私有Amazon API,但无法正常工作。我可以使用Postman成功进行呼叫,但是我试图使其与代码一起使用,但是失败了。

这是邮递员的屏幕截图:

node.js - Node.js中的AWS4签名-LMLPHP

这是试图复制此代码的代码:

request(aws4.sign({
    service: 'execute-api',
    region: 'us-east-1',
    method: 'POST',
    url: 'https://test.amazonAPI.com/test/doThing',
    body: load
  },
  {
    accessKeyId: tempCreds.Credentials.AccessKeyId,
    secretAccessKey: tempCreds.Credentials.SecretAccessKey,
    sessionToken: tempCreds.Credentials.SessionToken
  }))


我目前遇到的错误是:

Error: getaddrinfo ENOTFOUND execute-api.us-east-1.amazonaws.com execute-api.us-east-1.amazonaws.com:443
    at errnoException (dns.js:53:10)
    at GetAddrInfoReqWrap.onlookup [as oncomplete] (dns.js:95:26)

最佳答案

我认为您在requestOptions中缺少主机名。
正确:

request(aws4.sign({
    hostname: 'test.amazonAPI.com',
    service: 'execute-api',
    region: 'us-east-1',
    method: 'POST',
    url: 'https://test.amazonAPI.com/test/doThing', // this field is not recommended in the document.
    body: load
  },
  {
    accessKeyId: tempCreds.Credentials.AccessKeyId,
    secretAccessKey: tempCreds.Credentials.SecretAccessKey,
    sessionToken: tempCreds.Credentials.SessionToken
  }))


参考:https://github.com/mhart/aws4

09-30 00:39