2

我在 Bluemix 上创建了我的语言识别服务,并尝试使用 R 来调用它。

在此处输入图像描述

http://www.ibm.com/smarterplanet/us/en/ibmwatson/developercloud/apis/#!/language-identification/identify

这是我正在使用的代码:

library(httr)
login <- "https://gateway.watsonplatform.net/laser/service/api/v1/txtlid/bb0e9e07-cf44-4e95-a5a1-3fb0d53ac98f"
pars <- list(
  sid="lid-generic",
  txt="how are you"
)
POST(login, authenticate("my_username", "my_password@p"), body = pars)

我得到的响应当然不是预期的,错误 401。有谁知道我在这里做错了什么?

我从中得到的结果是:

Response [https://gateway.watsonplatform.net/laser/service/api/v1/txtlid/bb0e9e07-cf44-4e95-a5a1-3fb0d53ac98f]
  Date: 2015-01-23 12:29
  Status: 401
  Content-type: text/html
  Size: 252 B
--------------------------4bd32c1a987ed099
Content-Disposition: form-data; name="sid"

lid-generic
--------------------------4bd32c1a987ed099
Content-Disposition: form-data; name="txt"

how are you
--------------------------4bd32c1a987ed099--
4

2 回答 2

2

这里有一些可能有用的“R”代码片段: https ://github.com/rustyoldrake/R_Scripts_for_Watson

简而言之 - 下面的语法显示使用 getURL (RCurl) 和 POST (HTTR) 使用“username_password”进行身份验证

### Initialize Creds and URL
base_url = "https://gateway.watsonplatform.net/dialog-beta/api/v1" 
username = "9XXXXXX-XXXX-XXXX-XXXX-XXXXXXXXX" # from Bluemix Service Credentials
password = "123123123123"
username_password = paste(username,":",password,sep="")


###### FUNCTION - CHECK CLASSIFIER STATUS (DIALOG Example)
watson.nlc.checkclassifierstatus <- function(classifier_id) {
  return(
    getURL(paste(base_url,classifier_id,sep=""),userpwd = username_password)
  )
}

###### FUNCTION CREATE NEW CLASSIFIER  (NLC Example)
watson.nlc.createnewclassifier <- function(file,classifiername) {
  return(POST(url="https://gateway.watsonplatform.net/natural-language-classifier/api/v1/classifiers",
         authenticate(username,password),
         body = list(training_data = upload_file(file),
                     training_metadata = paste("{\"language\":\"en\",\"name\":",classifiername,"}",sep="") 
         )))}
于 2015-09-17T00:14:41.410 回答
1

http 401 表示身份验证问题;大概您有一个在创建服务实例时分配给它的用户 ID/密码。您需要将这些作为用户 ID/密码传递给 R 中的 HTTP 调用。

你似乎在你的代码中这样做:

POST(login, authenticate("my_username", "my_password@p"), body = pars)

我想知道您是否只是将错误的用户名/密码复制到了authenticate()调用中的这些值中。

于 2015-01-25T00:22:20.083 回答