使用IAM身份验证发送到API网关

问题描述 投票:-1回答:1

我如何向api网关发出发布请求并使用IAM进行身份验证?我只是想通过JSON作为正文。有没有一种简单的方法可以使用Alamofire做到这一点(不一定),但是我需要使用AWS签名(IAM)进行身份验证?到目前为止,我在网上找到的所有内容似乎都不过分。谢谢

ios swift amazon-web-services aws-api-gateway
1个回答
0
投票

在我们的情况下,API网关配置为请求API密钥和用户令牌。因此,我们在标头中传递了x-api-keyx-pool-token。这是内置的Foundation库的实现:

guard let url = URL(string: "https://your-api-gateway-url") else { return }
var request = URLRequest(url: url)
request.httpMethod = "POST"
var headerParameters: [String: String] = [
    "Content-Type": "application/json", // could be different, depends on your needs
    "Accept": "application/json", // could be different, depends on your needs
    "x-api-key": "your API key here" // API key, if Gateway is configured to request it
    "x-pool-token": "current valid user token here" // valid auth token from the logged in user
]
request.allHTTPHeaderFields = headerParameters
request.httpBody = //... (encoded body)

URLSession.shared.dataTask(with: request) { (data, response, err) in
    // process response here
© www.soinside.com 2019 - 2024. All rights reserved.