繁体   English   中英

Postman AWS Rest API — 作为 javascript?

[英]Postman AWS Rest API — as javascript?

我做了一个简单的 IAM 认证 API,它返回一个随机数。 [仅获取]
Postman 通话正常:
https://docs.aws.amazon.com/apigateway/latest/developerguide/how-to-use-postman-to-call-api.html?shortFooter=true

什么是让 postman 调用普通 javascript 的简单方法
(没有 npm 或 webpack)

非常感谢

You can use axios and aws4 library in javascript to make api calls and send a signed request respectively.You need to authenticate the user via Cognito and retrieve temporary credentials (access key, secret key, and session token)

let request = {
  host: 'myapi.execute-api.us-west-2.amazonaws.com',
  method: 'GET',
  url: 'https://myapi.execute-api.us-west-2.amazonaws.com/foo/bar',
  path: '/foo/bar'
}

let signedRequest = aws4.sign(request,
  {
    // assumes user has authenticated and we have called
    // AWS.config.credentials.get to retrieve keys and
    // session tokens
    secretAccessKey: AWS.config.credentials.secretAccessKey,
    accessKeyId: AWS.config.credentials.accessKeyId,
    sessionToken: AWS.config.credentials.sessionToken
  })

delete signedRequest.headers['Host']
delete signedRequest.headers['Content-Length']

let response = await axios(signedRequest)

本文可能会帮助您使用基本代码从 cognito 获取临时凭证并验证用户。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM