我正在尝试使用swift3在Flickr上传图片。请查看我的代码并提出建议。
以下是该服务的链接: -
flickr.com/services/api/upload.api.html
flickr.com/services/api/upload.example.html
我已经实现了Flickr的oauth,这是我在github上的示例项目的链接。 https://github.com/uditajmera/FlickSwift3Demo
public func uploadPhotosURLs1(lobjImageToUpload:UIImage)
{
let secret = FlickrManager.sharedInstance.strAuthSecret
//where secret is 7e5cfde9b0023627
let api_key = FlickrManager.sharedInstance.strApiKey
let auth_token = FlickrManager.sharedInstance.strAuthToken
let imageData = UIImageJPEGRepresentation(lobjImageToUpload, 1)
let uploadSig = "\(secret)_key\(api_key)_token\(auth_token)"
let request = NSMutableURLRequest()
let url = "http://api.flickr.com/services/upload/"
request.url = URL(string: url)!
request.httpMethod = "POST"
let boundary = String("---------------------------7d44e178b0434")
request.addValue("multipart/form-data; boundary=\(boundary)", forHTTPHeaderField: "Content-Type")
let body:NSMutableData = NSMutableData()
body.append("\r\n--\(boundary)\r\n".data(using: String.Encoding.utf8)!)
body.append("Content-Disposition: form-data; name=\"api_key\"\r\n\r\n".data(using: String.Encoding.utf8)!)
body.append("\(api_key)\r\n".data(using: String.Encoding.utf8)!)
body.append("--\(boundary)\r\n".data(using: String.Encoding.utf8)!)
body.append("Content-Disposition: form-data; name=\"auth_token\"\r\n\r\n".data(using: String.Encoding.utf8)!)
body.append("\(auth_token)\r\n".data(using: String.Encoding.utf8)!)
body.append("--\(boundary)\r\n".data(using: String.Encoding.utf8)!)
body.append("Content-Disposition: form-data; name=\"api_sig\"\r\n\r\n".data(using: String.Encoding.utf8)!)
body.append("\(uploadSig)\r\n".data(using: String.Encoding.utf8)!)
body.append("--\(boundary)\r\n".data(using: String.Encoding.utf8)!)
body.append(String("Content-Disposition: form-data; name=\"photo\"; filename=\"photo.jpg\"\r\n").data(using: String.Encoding.utf8)!)
body.append("Content-Type: image/jpeg\r\n\r\n".data(using: String.Encoding.utf8)!)
body.append(imageData!)
body.append("\r\n--\(boundary)--\r\n".data(using: String.Encoding.utf8)!)
request.httpBody = body as Data
let session = URLSession.shared
let task = session.dataTask(with: request as URLRequest,
completionHandler: {(data, response, error) in
if let error = error {
print(error)
}
if let data = data{
print("data =\(data)")
}
if let response = response {
print("url = \(response.url!)")
print("response = \(response)")
let httpResponse = response as! HTTPURLResponse
print("response code = \(httpResponse.statusCode)")
print("DATA = \(data)")
//if you response is json do the following
do{
let resultJSON = try JSONSerialization.jsonObject(with: data!, options: JSONSerialization.ReadingOptions())
let arrayJSON = resultJSON as! NSArray
for value in arrayJSON{
let dicValue = value as! NSDictionary
for (key, value) in dicValue {
print("key = \(key)")
print("value = \(value)")
}
}
}catch _{
print("Received not-well-formatted JSON")
}
}
})
task.resume()
}
预期回复采用XML格式: - “< photoid> 1234< / photoid>”
获得此回复: -
response =
<NSHTTPURLResponse: 0x618000223c00> {
URL:http://api.flickr.com/services/upload/ } { status code: 200, headers {
Age = 14;
"Cache-Control" = private;
Connection = "keep-alive";
"Content-Encoding" = gzip;
"Content-Length" = 117;
"Content-Type" = "text/xml; charset=utf-8";
Date = "Tue, 13 Dec 2016 07:55:13 GMT";
P3P = "policyref=\"https://policies.yahoo.com/w3c/p3p.xml\", CP=\"CAO DSP COR CUR ADM DEV TAI PSA PSD IVAi IVDi CONi TELo OTPi OUR DELi SAMi OTRi UNRi PUBi IND PHY ONL UNI PUR FIN COM NAV INT DEM CNT STA POL HEA PRE LOC GOV\"";
Server = ATS;
Vary = "Accept-Encoding";
Via = "http/1.1 r01.ycpi.inc.yahoo.net (ApacheTrafficServer [cMsSf ])";
"X-Frame-Options" = SAMEORIGIN;
"X-Robots-Tag" = noindex;
"X-Served-By" = "up8.flickr.bf1.yahoo.com";
} }