스위치를 켜기 위해 범용 장치 허브에 REST API 호출을하려고합니다. 통화가 진행되는 것처럼 보이지만 인터페이스에 들어가기 위해 필요한 자격 증명이 있기 때문에 자격 증명이 필요하다는 오류가 표시됩니다. 그러나 나는이 일을하는 방법을 모르겠습니다.일치하는 신용을 복사하는 중 오류가 발생했습니다 - Swift (REST API 호출)
내 코드는 내가 온라인으로 함께 몇 가지 예제를 조립할 시도 다음
class ViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
@IBOutlet weak var lightOn: UIButton!
@IBAction func lightOn(_ sender: Any) {
guard let url = URL(string: "http://0.0.0.0/rest/nodes/ZW002_1/cmd/DFON") else { return }
let userCredential = URLCredential(user: "admin",
password: "admin",
persistence: .permanent)
URLCredentialStorage.shared.setDefaultCredential(userCredential, for: protectionSpace)
// create URL session ~ defaulted to GET
let session = URLSession.shared
session.dataTask(with: url) { (data, response, error) in
// optional chaining to make sure value is inside returnables and not not
if let response = response {
print(response)
}
if let data = data {
// assuming the data coming back is Json -> transform bytes into readable json data
do {
let json = try JSONSerialization.jsonObject(with: data, options: [])
print(json)
} catch {
print("error")
}
}
}.resume() // if this is not called this block of code isnt executed
}
}
이며, 사람은 내가 사용 protectionSpace을 보았다하지만 난 그것을 코드를 반환 사용할 때 : 또한
Use of unresolved identifier 'protectionSpace'
을 전반적으로 시뮬레이터를 실행할 때마다 정확한 오류가 발생합니다.
2017-12-26 13:28:58.656122-0600 hohmtest[6922:1000481] CredStore - performQuery - Error copying matching creds. Error=-25300, query={
atyp = http;
class = inet;
"m_Limit" = "m_LimitAll";
ptcl = http;
"r_Attributes" = 1;
sdmn = "/";
srvr = "192.168.1.73";
sync = syna;
}
<NSHTTPURLResponse: 0x60400042a3e0>
{ URL:
http://192.168.1.73/rest/nodes/ZW002_1/cmd/DON/ } { Status Code: 401,
Headers {
"Cache-Control" = (
"max-age=3600, must-revalidate"
);
Connection = (
"Keep-Alive"
);
"Content-Length" = (
0
);
"Content-Type" = (
"text/html; charset=UTF-8"
);
EXT = (
"UCoS, UPnP/1.0, UDI/1.0"
);
"Last-Modified" = (
"Tue, 26 Dec 2017 11:26:15 GMT"
);
"Www-Authenticate" = (
"Basic realm=\"/\""
);
} }
error
API에 대한 설명서가 없지만 엔드 포인트에서 URLCredential을 사용하지 않을 것으로 예상됩니다. 일반적으로 헤더 나 본문을 통해 userId/PW를 전송하는 API가 표시되며 토큰을 다시 보냅니다. 그런 다음 후속 API 호출을 위해 본문을 통해 토큰을 BACK에 보냅니다. – ghostatron