1
if (!require("httr")) {
  install.packages("httr", repos="http://cran.rstudio.com/")
  library("httr")
}

if (!require("jsonlite")) {
  install.packages("jsonlite", repos="http://cran.rstudio.com/")
  library("jsonlite")
}

我的 Get 工作正常...

api <- "xxxxxxxxxxxxxxxx"
url <-paste0(
   "https://translation.googleapis.com/language/translate/v2/languages/? 
   key=",api
 )
data3 <- GET(url)
data3 <- content(data3)
data3 <- as.data.frame(data3)
data3 <- as.data.frame(t(data3))

我的 POST 不起作用

url <- paste0(
   "https://translation.googleapis.com/language/translate/v2/detect/?key=",api
 )
request_body <- data.frame(message = "hello")
request_body_json <- toJSON(list(documents = request_body), auto_unbox = TRUE)
result <- POST(url, body = request_body_json)

我试图让语言检测工作我的理解是我们需要在 JSON 文件中发布到谷歌 API 但是我收到错误 #"code": 400, #"message": "Required Text"

我希望有人能指出我正确的方向我知道有一个 R 包但我正在尝试学习如何在没有包的情况下做到这一点,因为有时谷歌或微软会更新他们的 API 并且 # 包没有得到更新(示例:自 API 更新以来,TranslateR 不支持 Microsoft,因此我想尽可能多地学习如何手动执行此操作 # https://cloud.google.com/translate/docs/quickstart?csw=1 https: //translation.googleapis.com/ $discovery/rest?version=v2

4

1 回答 1

1

没关系...经过几个小时的黑客攻击,我想通了.. =)

TEXT1 = "Testate con lo smartphone si sente decisamente bene"
TEXT1 <- str_replace_all(TEXT1, " ", "%20")
test1 <- paste0("https://translation.googleapis.com/language/translate/v2?target=en&key=xxxxxxxxx&q=", TEXT1)
result2 <- POST(test1)
result2 <- content(result2)
result2 <- as.data.frame(result2)
于 2018-06-11T16:39:41.387 回答