问题
I am trying to post to the Watson tone analyzer API using AlamoFire with the following code. It keeps getting a 401 error which apparently means failed authorization. The same userid/password info, however, works find with a curl request. So the problem does not seem to be with the userid/password but rather with how I am forming the AlamoFire request.
func postToWatson () {
print("post to watson called")
let url: String = "https://gateway.watsonplatform.net/tone-analyzer/api/v3/tone?version=2016-05-19"
let message = "All you need is love"
var parameters = [
"username":"my-lengthy-username",
"password":"my-ugly-password"]
parameters["text"] = message
Alamofire.request(url, parameters: parameters)
.responseJSON { response in
print(response.request)
print(response.response)
print(response.result)
}
}
The following is what I get back from the API via the above print commands:
Optional(https://gateway.watsonplatform.net/tone-analyzer/api/v3/tone?version=2016-05-19&password=xxxxxx&text=All%20you%20need%20is%20love&username=xxxxxxxxxxxx)
Optional(<NSHTTPURLResponse: 0x1740396a0> { URL: https://gateway.watsonplatform.net/tone-analyzer/api/v3/tone?version=2016-05-19&password=xxxxxx&text=All%20you%20need%20is%20love&username=xxxxxxxxxxxx } { status code: 401, headers {
Connection = close;
"Content-Encoding" = gzip;
Date = "Wed, 30 May 2018 17:23:30 GMT";
"Strict-Transport-Security" = "max-age=31536000;";
"Www-Authenticate" = "Basic realm=\"IBM Watson Gateway(Log-in)\"";
"X-Backside-Transport" = "OK OK,FAIL FAIL";
"X-DP-Transit-ID" = "gateway02-1020566241";
"X-DP-Watson-Tran-ID" = "gateway02-1020566241";
"X-Global-Transaction-ID" = ffea405d5b0ede123cd49ae1;
"x-dp-local-file" = true;
} })
SUCCESS
What is wrong with the code above?
回答1:
I haven't used AlamoFire, but looking at their documentation for making a request the authentication is handled differently from your code.
The username / password aren't regular parameters, but you need to pass them as authentication header. The docs have samples for that. That would explain the 401, because no authentication is passed to Watson.
Not tested, but something like this should work:
Alamofire.request(url)
.authenticate(user: username, password: password)
.responseJSON { response in
print(response.request)
print(response.response)
print(response.result)
}
来源:https://stackoverflow.com/questions/50610581/ios-ibm-cloud-swift-post-to-watson-api-using-alamofire