我正在尝试使用来自 Swift 2.2 程序的 API 从 FreshDesk 检索所有票证(这是 API)
以下卷曲有效:
curl -v -u myEmail@example.com:myPassword -X GET 'https://mydomain.freshdesk.com/api/v2/tickets'
我创建了这个函数来检索票:
func getAllTickets() {
let username = "myEmail@example.com"
let password = "myPassword"
let loginString = "\(username):\(password)"
let loginData = loginString.data(using: .utf8)
let base64LoginString = loginData?.base64EncodedString(options: [])
if let url = NSURL(string: "https://mydomain.freshdesk.com/api/v2/tickets"){
let request = NSMutableURLRequest(url: url as URL)
request.httpMethod = "GET"
request.setValue("Basic \(base64LoginString)", forHTTPHeaderField: "Authorization")
let session = URLSession.shared
session.dataTask(with: request as URLRequest, completionHandler: { (returnData, response, error) -> Void in
if let error = error {
// couldn't even make the call - probably no network...
// maybe save it in the DB for next time?
print("Error connecting to Freshdesk API - error is: \(error.localizedDescription)")
if error.localizedDescription == "The Internet connection appears to be offline" {
// TODO - save error up until next time
}
return
}
let strData = NSString(data: returnData!, encoding: String.Encoding.utf8.rawValue)
print("GOT RESULT: \(strData)")
}).resume()
}
}
我得到的输出是: GOT RESULT: Optional({"code":" invalid_credentials ","message":" You have to be logged in to perform this action ."})
但我确信用户名/密码是正确的,因为 curl 有效