我需要在我的ios应用程序中使用WP REST API,现在我正在使用Alamofire和SwiftyJSON。
func requestAlamofireToSwiftyJSON(_ url:String, completion: @escaping (JSON?, Bool, Success) -> ()) {
let manager = Alamofire.SessionManager.default
manager.session.configuration.timeoutIntervalForRequest = TIMEOUT
manager.session.configuration.timeoutIntervalForResource = TIMEOUT
manager.request(url).responseJSON { (response) -> Void in
switch response.result {
case .success:
guard let value = response.result.value else {
return
}
let json = JSON(value)
DLog(message:json)
completion(json, true, .loadOK)
case .failure(let error):
DLog(message:"failure")
DLog(message:"\n\nAuth request failed with error:\n \(error)")
if error._code == NSURLErrorTimedOut {
//HANDLE TIMEOUT HERE
completion(nil, false, .timeOut)
} else {
completion(nil, false, .loadError)
}
}
}
}
日志:
{
"code" : "rest_cannot_access",
"data" : {
"status" : 401
},
"message" : "Only authenticated users can access the REST API."
}
根据WP rest API documentation,我需要认识我,但我不知道如何在SWIFT中这样做。
编辑1
差不多:) 为什么此代码有效? JSON显示在控制台中。
var headers: HTTPHeaders = [
"Content-Type": "application/json"
]
let user = "userblablabla"
let password = "pwdblablabla"
if let authorizationHeader = Request.authorizationHeader(user: user, password: password) {
headers[authorizationHeader.key] = authorizationHeader.value
}
Alamofire.request(url, headers:headers).responseJSON{ response in
DLog(message:url)
switch response.result {
case .success:
guard let value = response.result.value else {
return
}
let json = JSON(value)
DLog(message:json)
completion(json, true, .loadOK)
case .failure(let error):
DLog(message:"failure")
DLog(message:"\n\nAuth request failed with error:\n \(error)")
if error._code == NSURLErrorTimedOut {
//HANDLE TIMEOUT HERE
completion(nil, false, .timeOut)
} else {
completion(nil, false, .loadError)
}
}
}
为什么此代码不起作用? 错误401(见上文)
let headers = [ "Content-Type": "application/json","X-Requested-With": "XMLHttpRequest","Cache-Control": "no-cache"]
let manager = Alamofire.SessionManager.default
manager.session.configuration.timeoutIntervalForRequest = TIMEOUT
manager.session.configuration.timeoutIntervalForResource = TIMEOUT
manager.request(url).authenticate(user: "userblablabla", password: "pwdblablabla").responseJSON { (response) -> Void in
switch response.result {
case .success:
guard let value = response.result.value else {
return
}
let json = JSON(value)
DLog(message:json)
completion(json, true, .loadOK)
case .failure(let error):
DLog(message:"failure")
DLog(message:"\n\nAuth request failed with error:\n \(error)")
if error._code == NSURLErrorTimedOut {
//HANDLE TIMEOUT HERE
completion(nil, false, .timeOut)
} else {
completion(nil, false, .loadError)
}
}
}
答案 0 :(得分:1)
您似乎缺少使用用户名和密码访问数据的标头。我可以提到的另一件事是确保将其设为私有。否则其他用户可以访问wordpress调用,也可以编辑帖子和内容。
var headers: HTTPHeaders = [:]
// Pass in the username and the password in the authorizationHeader
if let authorizationHeader = Request.authorizationHeader(user: YOURUSERNAME, password: YOURPASSWORD) {
headers[authorizationHeader.key] = authorizationHeader.value
}
// Request with headers
Alamofire.request(url, method: .get, headers: headers).responseJSON { (response) in
switch response.result {
case .success(let value):
// Handle success
case .failure(let error):
// Handle error
}
}
答案 1 :(得分:1)
那么@cmii,API调用中的指针很少,你应该考虑:
根据您的问题,您需要传递一些带有用户访问令牌的请求标头。我在我的一个项目中使用了以下标题:
headers = [ "Content-Type": "application/json",
"X-Requested-With": "XMLHttpRequest"
"Cache-Control": "no-cache",
"Authorization": "bearer " + "KJF73RWHFI23R" ]
从您分享的链接wordpress。您需要在请求标头中设置X-WP-Nonce
。
$.ajax( {
url: wpApiSettings.root + 'wp/v2/posts/1',
method: 'POST',
beforeSend: function ( xhr ) {
xhr.setRequestHeader( 'X-WP-Nonce', wpApiSettings.nonce );
},
data:{
'title' : 'Hello Moon'
}
} ).done( function ( response ) {
console.log( response );
} );
除了 url 。 Alamofire.SessionManager
请求中的其他参数很少:
您可以将响应状态代码验证为validate(statusCode: 200..<300)
,仅当响应状态代码的范围为200到300时,它才会成功。
您的更新代码如下所示:
let manager = Alamofire.SessionManager.default
manager.session.configuration.timeoutIntervalForRequest = TIMEOUT
manager.session.configuration.timeoutIntervalForResource = TIMEOUT
// USE methods, parameters, encoding and headers as per your requirement
// for example
// manager.request(url, method: .get, headers: <header dictionary>)
manager.request(url, method: .post, parameters: [:], encoding: JSONEncoding.default, headers: [:]).validate(statusCode: 200..<300).responseJSON { (response) in
switch response.result {
case .success:
guard let value = response.result.value else {
return
}
let json = JSON(value)
DLog(message:json)
completion(json, true, .loadOK)
case .failure(let error):
DLog(message:"failure")
DLog(message:"\n\nAuth request failed with error:\n \(error)")
if error._code == NSURLErrorTimedOut {
//HANDLE TIMEOUT HERE
completion(nil, false, .timeOut)
} else {
completion(nil, false, .loadError)
}
}
}