R:谷歌翻译API(包'translate'&''translateR')

时间:2016-07-15 16:03:35

标签: r google-translate translate

我正在尝试将translatetranslateR个软件包与R-Studio一起使用。

我创建了“服务器”和“浏览器”API密钥。运行示例时,浏览器API工作正常:

https://www.googleapis.com/language/translate/v2?key=YOUR_API_KEY&q=hello%20world&source=en&target=de

但是,当使用API​​密钥和R-Studio包(translate / translateR)时,我会收到错误消息。使用translate

> library(translate)
> set.key("mykey")
> translate('Hello, world!', 'en', 'de')
Error in function (type, msg, asError = TRUE)  : 
  SSL certificate problem: unable to get local issuer certificate

可能是什么问题?谢谢你的帮助!

2 个答案:

答案 0 :(得分:1)

似乎问题与系统有关。它在我更改https代理后工作。

答案 1 :(得分:0)

我也遇到了一些问题并写了一个小函数来从API中检索数据:

#' Translate with R
#'
#' Translate Keywords or/and text with the Google Translate API
#' The Functions allows to translate keywords or sentences using the Google Translate API.
#' To use this function you need to get a API-Key for the Google Translate API <https://cloud.google.com/translate/docs/?hl=en>.
#' @param text The keyword/sentence/text you want to translate
#' @param API_Key Your API Key. You get the API Key here: <https://cloud.google.com/translate/docs/?hl=en>
#' @param target The Language target your text translated to. For German 'de'. 
#' @param source The Language your given text/keyword is. For example 'en' - english 
#' translate()
#' @examples
#' \dontrun{
#' translate(text = "R is cool", API_Key = "XXXXXXXXXXX", target = "de", source = "en")
#' }


translate <- function(text,
                      API_Key,
                      target = "de",
                      source = "en") {
  b <- paste0(
    '{
    "q": [
    "',
    text,
    '"
    ],
    "target": "',
    target,
    '",
    "source": "',
    source,
    '",
    "format": "text"
}'
)
  url <-
    paste0("https://translation.googleapis.com/language/translate/v2?key=",
           API_Key)
  x <- httr::POST(url, body = b)
  x <- jsonlite::fromJSON(rawToChar(x$content))
  x <- x$data$translations
  return(x$translatedText[1])
  }

此处更新了要点:https://gist.github.com/dschmeh/8414b63c3ab816c44995cd6872165f0e