首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >R: google translate API (包'translate‘和’translate‘)

R: google translate API (包'translate‘和’translate‘)
EN

Stack Overflow用户
提问于 2016-07-16 00:03:35
回答 2查看 11.3K关注 0票数 0

我正在尝试在R-Studio中使用translatetranslateR包。

我已经创建了“服务器”和“浏览器”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

代码语言:javascript
复制
> 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

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

EN

回答 2

Stack Overflow用户

发布于 2016-07-21 01:26:00

似乎这个问题与系统有关。在我更改了https代理之后,它就可以工作了。

票数 1
EN

Stack Overflow用户

发布于 2018-01-23 16:36:08

我在这方面也遇到了一些问题,并编写了一个小函数来从API中检索数据:

代码语言:javascript
复制
#' 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

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/38400582

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档