iOS / IBM Cloud / Swift:使用AlamoFire发布到Watson API [英] iOS/IBM Cloud/Swift: Post to Watson API using AlamoFire

查看:83
本文介绍了iOS / IBM Cloud / Swift:使用AlamoFire发布到Watson API的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用AlamoFire将以下代码发布到Watson音频分析器API。它不断收到401错误,这显然意味着授权失败。但是,可以使用curl请求找到相同的用户ID /密码信息。因此,问题似乎不在于用户名/密码,而在于我如何形成AlamoFire请求。

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)
        }
    }

以下是我通过上述打印命令从API中获得的信息:

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?

推荐答案

我没有使用AlamoFire,而是查看他们的用于提出身份验证请求的文档的处理方式与您的代码不同。

I haven't used AlamoFire, but looking at their documentation for making a request the authentication is handled differently from your code.

用户名/密码不是常规参数,但是您需要将其作为身份验证标头传递。该文档有示例。这将解释401,因为没有身份验证传递给Watson。

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)
}

这篇关于iOS / IBM Cloud / Swift:使用AlamoFire发布到Watson API的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆