Node.js中的AWS4签名

时间:2017-10-17 18:49:41

标签: node.js amazon-web-services aws-sdk-js

我正在尝试使用aws4程序包使用Javascript调用私有Amazon API,但我无法让它工作。我能够成功地与Postman进行通话,但我正试图让它与代码一起工作,而我却失败了。

这是邮递员截图:

enter image description here

以下是试图复制此内容的代码:

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)

1 个答案:

答案 0 :(得分:2)

我认为您在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