2

我正在尝试使用 aws4 包通过 Javascript 调用私有 Amazon API,但我无法让它工作。我可以使用 Postman 成功拨打电话,但我正试图让它与代码一起使用,但我失败了。

这是邮递员的截图:

在此处输入图像描述

这是试图复制它的代码:

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)
4

1 回答 1

3

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

于 2018-10-10T09:26:29.257 回答