I'm trying to retrieve all tickets from FreshDesk using their API from a Swift 2.2 program (Here's the API)
The following curl works:
curl -v -u myEmail@example.com:myPassword -X GET 'https://mydomain.freshdesk.com/api/v2/tickets'
and I've created this function to retrieve the 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()
}
}
The output I get is: GOT RESULT: Optional({"code":"invalid_credentials","message":"You have to be logged in to perform this action."})
But I'm sure the username/password is correct given that the curl works