我添加了一个
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密钥之前,该方法正在工作