我正在尝试使用swift实现GetItem operation但它总是返回401.以下是代码片段。目前正在getItem
调用viewDidLoad
。
func getItem() {
let url = URL(string: "https://outlook.office365.com/EWS/Exchange.asmx")!
let message = xmlString()
let data = message.data(using: .utf8)
var request = URLRequest(url: url)
request.httpMethod = "POST"
request.httpBody = data
request.addValue("text/xml; charset=utf-8", forHTTPHeaderField: "Content-Type")
request.addValue("\(data!.count)", forHTTPHeaderField: "Content-Length")
let confirguration = URLSessionConfiguration.default
let session = URLSession(configuration: confirguration, delegate: self, delegateQueue: OperationQueue.main)
let task = session.dataTask(with: request) { (data, response, error) in
print("Data: \(data!) ")
print("Response: \(response!)")
}
task.resume()
}
func urlSession(_ session: URLSession, didReceive challenge: URLAuthenticationChallenge, completionHandler: @escaping (URLSession.AuthChallengeDisposition, URLCredential?) -> Void) {
let userName = "xxxxxxx"
let password = "xxxxxxx"
let credential = URLCredential(user: userName, password: password, persistence: .forSession)
challenge.sender?.use(credential, for: challenge)
completionHandler(URLSession.AuthChallengeDisposition.useCredential, URLCredential(trust: challenge.protectionSpace.serverTrust!))
}
控制台输出:
2018-04-07 20:30:50.559884+0530 Tapas_EWSGetItem[2562:80677] CredStore - performQuery - Error copying matching creds. Error=-25300, query={
class = inet;
"m_Limit" = "m_LimitAll";
ptcl = htps;
"r_Attributes" = 1;
sdmn = "";
srvr = "outlook.office365.com";
sync = syna;
}
Data: 0 bytes
Response: <NSHTTPURLResponse: 0x608000223a40> { URL: https://outlook.office365.com/EWS/Exchange.asmx } { Status Code: 401, Headers {
"Content-Length" = (
0
);
Date = (
"Sat, 07 Apr 2018 15:00:44 GMT"
);
Server = (
"Microsoft-IIS/10.0"
);
"Www-Authenticate" = (
"Basic Realm=\"\""
);
"X-FEServer" = (
SG2PR06CA0201
);
"X-Powered-By" = (
"ASP.NET"
);
"request-id" = (
"7c7075b7-7496-4847-9418-d11e66a7c97f"
);
} }
任何人都可以帮我弄清楚我做错了什么。提前谢谢。