我在我的API中添加了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密钥之前,该方法仍然有效
答案 0 :(得分:1)