I am trying to upload image to Flickr from my app. I have referred some Objective-C code to do so but with no success.
The code I am using currently is:
func calUploadImageAPI(image:UIImage){ DispatchQueue.main.async(execute: { }) var resultFromServer: Any? var body : Data = Data() var boundry = String() boundry = NSUUID().uuidString // Request // var request = URLRequest(url:URL(string:"https://up.flickr.com/services/upload/")!) request.setValue("multipart/form-data; boundary=\(boundry)", forHTTPHeaderField: "Content-Type") request.timeoutInterval = 150.0 request.httpMethod="POST" var fileData = NSData() fileData = UIImagePNGRepresentation(image)! as NSData //proceed with resume upload let md5Data = MD5(string:"\(userAuthSecret)api_key\(userKey)auth_token\(userAuthToken)") let md5Hex = md5Data!.map { String(format: "%02hhx", $0) }.joined() body.append(String("--\(boundry)\r\n").data(using: String.Encoding.utf8)!) body.append(String("Content-Disposition: form-data; name=\"api_key\"\r\n\r\n\(userKey)\r\n").data(using: String.Encoding.utf8)!) body.append(String("--\(boundry)\r\n").data(using: String.Encoding.utf8)!) body.append(String("Content-Disposition: form-data; name=\"auth_token\"\r\n\r\n\(userAuthToken)\r\n").data(using: String.Encoding.utf8)!) body.append(String("--\(boundry)\r\n").data(using: String.Encoding.utf8)!) body.append(String("Content-Disposition: form-data; name=\"api_sig\"\r\n\r\n\(md5Hex)\r\n").data(using: String.Encoding.utf8)!) body.append(String("--\(boundry)\r\n").data(using: String.Encoding.utf8)!) body.append(String("Content-Disposition: form-data; name=\"\("photo")\"; filename=\"\("profile_image.png")\"\r\n").data(using: String.Encoding.utf8)!) body.append(String("Content-Type: image/jpeg\r\n\r\n").data(using: String.Encoding.utf8)!) body.append(fileData as Data) body.append(String("\r\n").data(using: String.Encoding.utf8)!) body.append(String("--\(boundry)--\r\n").data(using: String.Encoding.utf8)!) request.httpBody = body //session let configuration = URLSessionConfiguration.default configuration.requestCachePolicy = .reloadIgnoringCacheData let session : URLSession session = URLSession(configuration: configuration, delegate: nil, delegateQueue: nil) session.dataTask(with: request) { (data, resp, error) -> Void in if error != nil { DispatchQueue.main.async(execute: { }) }else { if data != nil { let httpResponse: HTTPURLResponse = resp as! HTTPURLResponse if httpResponse.statusCode == 200 || httpResponse.statusCode == 201 || httpResponse.statusCode == 202 || httpResponse.statusCode == 204 { do { resultFromServer = try JSONSerialization.jsonObject(with: data!, options: JSONSerialization.ReadingOptions.mutableContainers) if let respArr = resultFromServer as? NSArray{ print("Resp Array:\(respArr)") } if let respDict = resultFromServer as? NSDictionary{ print("Resp Dict:\(respDict)") } //post noif DispatchQueue.main.async(execute: { //post notif //no image? get newone // NSNotificationCenter.defaultCenter().postNotificationName(downloadimageURLNotif, object: nil) }) } catch let error as NSError { print("ERROR: \(error.description)") DispatchQueue.main.async(execute: { }) } } else if httpResponse.statusCode == 401 || httpResponse.statusCode == 403 || httpResponse.statusCode == 498 || httpResponse.statusCode == 400 { //get msg do { resultFromServer = try JSONSerialization.jsonObject(with: data!, options: JSONSerialization.ReadingOptions.mutableContainers) if let respArr = resultFromServer as? NSArray{ print("Resp Array:\(respArr)") } if let respDict = resultFromServer as? NSDictionary{ print("Resp Dict:\(respDict)") } DispatchQueue.main.async(execute: { }) } catch let error as NSError { print("ERROR: \(error.description)") DispatchQueue.main.async(execute: { }) } } } } }.resume()}
And the md5 method is
func MD5(string: String) -> Data? { guard let messageData = string.data(using:String.Encoding.utf8) else { return nil } var digestData = Data(count: Int(CC_MD5_DIGEST_LENGTH)) _ = digestData.withUnsafeMutableBytes {digestBytes in messageData.withUnsafeBytes {messageBytes in CC_MD5(messageBytes, CC_LONG(messageData.count), digestBytes) } } return digestData }
But I get the following error:
"<?xml version=\"1.0\" encoding=\"utf-8\" ?>\n<rsp stat=\"fail\">\n\t<err code=\"98\" msg=\"Invalid auth token\" />\n</rsp>\n"
But I have cross checked and my tokens are correct. I obtain them using OAuthSwift project.