代码之家  ›  专栏  ›  技术社区  ›  Wizzardzz

Swift API网关密钥授权

  •  0
  • Wizzardzz  · 技术社区  · 6 年前

    我添加了一个 key 但我很难在我的应用程序代码中实现它。我不断得到 message = Forbidden;

    我已经测试过不同的方法,但都不管用。这里怎么了?

    if let jsonData = try? JSONSerialization.data(withJSONObject: dict, options: .prettyPrinted) {
    
            let url = NSURL(string: "\(APIAddress)")!
            let request = NSMutableURLRequest(url: url as URL)
    
            request.httpMethod = "PATCH"
            request.addValue("application/json", forHTTPHeaderField: "Content-Type")
            request.addValue("\(APIKey)", forHTTPHeaderField: "Authorization")
            request.httpBody = jsonData
    
            let task = URLSession.shared.dataTask(with: request as URLRequest) { data, response, error in
                if error != nil {
                    print(error?.localizedDescription as Any)
                    return
                }
    
                do {
                    let json = try JSONSerialization.jsonObject(with: data!, options: .mutableContainers) as? NSDictionary
    
                    if let parseJSON = json {
                        let resultValue = parseJSON["success"]
                        print("result: \(String(describing: resultValue))")
                        print(parseJSON)
                    }
                } catch let error as NSError {
                    print(error)
                }
            }
            task.resume()
        }
    

    请注意,在API网关上激活API密钥之前,该方法正在工作

    1 回复  |  直到 6 年前
        1
  •  1
  •   MQLN Ahmed Sbai    6 年前

    您没有包括 POST 要求

    Here're the docs on how to construct a valid request

    看来你错过了 host x-amz-date 标题。