r - 从 R 调用 Watson API

标签 r ibm-cloud ibm-watson

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

enter image description here

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

最佳答案

这里有一些可能有用的“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="") 
         )))}

关于r - 从 R 调用 Watson API,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28116511/

相关文章:

r - 使用 ggplot 在直方图上绘制不同的分布

r - 从 R 中的 princomp 加载构建分数

R 正则表达式 : modifying digits of varying lengths at end of string

php - 如何在 Bluemix php 中部署自定义骨架应用程序

db2 - 我是否必须在 IBM Cloud 中配置付费数据库才能使用 Watson OpenScale 的功能?

curl - 文本的Watson语音返回有关文件大小的奇怪错误

r - 将值添加到R中表的条形图中

java - 如何让 Watson Conversation Api 在用户输入时调用 Web 应用程序 Url?

node.js - Watson视觉识别运行错误

java - Watson视觉识别,在java中针对多个分类器进行分类