无法验证 google 在 R 中翻译

failed to authenticate google translate in R

所以我尝试在 Rstudio 中使用 gl_translate 函数 到 500,000 个字符,这意味着我必须验证我的 google 翻译 API .问题是我两个月前用我的旧 google 帐户尝试过,现在我正在使用新帐户。

因此,当我尝试使用我的新 google 帐户对新 client_id 进行身份验证时,我收到错误消息,指出我的 API 没有尚未启用,我已启用它。我重新启动了我的 Rstudio,现在我收到了这条错误消息:

020-01-22 19:01:24 -- Translating html: 147 characters - 
2020-01-22 19:01:24> Request Status Code: 403
Error: API returned: Request had insufficient authentication scopes.

这非常令人沮丧,因为当时我尝试启用旧的 google 帐户,它要求我输入我的信用卡号,我又这样做了,现在他们让我等几天。

谁能知道这有什么问题?

这是我用于验证的 R 代码:

install.packages("googleAnalyticsR", dependencies = TRUE)
library(googleAnalyticsR)

install.packages("googleLanguageR")
library(googleLanguageR)

install.packages("dplyr")
library(dplyr)

library(tidyverse)

install.packages("googleAuthR")
library(googleAuthR)


client_id <- "107033903887214478396"
private_key <- "-----BEGIN PRIVATE KEY-----\nMIIEvAIBADANBgkqhkiG9w0BAQEFAASCBKYwggSiAgEAAoIBAQChPmvib1v9/CFA\nX7fG8b8iXyS370ivkufMmX30C6/rUNOttA+zMhamS/EO0uaYtPw44B4QdNzRsSGq\nm+0fQ5Sp1SHJaVPkoImuUXZdMlLO73pvY48nMmEFg7deoOZI+CNWZYgIvPY8whMo\nk4vKE2iyuG+pl9MT7dT6dwWNmXDWr8kgxAfryfVEUIeqaf+57Z9g3FfVPLARz4iH\nCaTu55hhbmo/XknUx0hPsrwBMPaNLGl2+o5MU1ZuIkl47EJvdL8CdUuEmb9qJDtv\nUyKqANlwFa7cVW8ij2tjFpjJ7bigRVJsI8odbsEbwmx1b5SLckDOQ7t4l8zERtmo\nUv5fxyNNAgMBAAECggEAApeLyWyL2IXcjPnc7OxG68kGwJQuoW/lnQLcpPcpIUm/\n1Vt/IxzLg2nWGqxmO48xPMLRiOcwA4jq5yCxi56c/avo6qFwUU0JWY2Crx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P/KDBlJkOsOvTkmJ4TxbIhgcSI62/wqDBi5Xqw1ljR2mh8njzRwqDRKs12EtQ0\n9VaUDm7LCNTAskn2SR/o4Q==\n-----END PRIVATE KEY-----\n"

options(googleAuthR.client_id = client_id)
options(googleAuthR.client_secret =  private_key)

devtools::reload(pkg = devtools::inst("googleAnalyticsR"))
ga_auth()

如果您需要查看我的翻译代码,例如:

translate <- function(tibble) {

tibble <- tibble
count <- data.frame(nchar = 0, cumsum = 0) # create count file to stay within API limits

for (i in 1:nrow(tibble)) {

des <- pull(tibble[i,2]) # extract description as single character string

if (count$cumsum[nrow(count)] >= 80000) { # API limit check

  print("nearing 100000 character per 100 seconds limit, pausing for 100 seconds")

  Sys.sleep(100)

  count <- count[1,] # reset count file
}

if (grepl("^\s*$", des) == TRUE) { # if description is only whitespace then skip

  trns <- tibble(translatedText = "", detectedSourceLanguage = "", text = "")

  } else { # else request translation from API

    trns <- gl_translate(des, target='en', format='html') # request in html format to anticipate html descriptions
}


tibble[i,3:4] <- trns[,1:2] # add to tibble

nchar = nchar(pull(tibble[i,2])) # count number of characters

req <- data.frame(nchar = nchar, cumsum = nchar + sum(count$nchar))

count <- rbind(count, req) # add to count file

if (nchar > 20000) { # addtional API request limit safeguard for large descriptions

  print("large description (>20,000), pausing to manage API limit")

  Sys.sleep(100)

  count <- count[1,] # reset count file
 }
}
return(tibble)
}

24小时后我搞定了

看来真的很简单。我只是按照 link 的步骤操作。 但是昨天我弄错了,因为我下载的 json 文件是来自 service client Id 的 json 文件,而我实际上需要来自 json 的文件服务帐号.

然后我使用以下代码安装 googleLanguageR 包:

remotes::install_github("ropensci/googleLanguageR")
library(googleLanguageR)

然后只需在 GL_AUTH 参数中设置我的下载 Google 项目 JSON 文件的文件位置,如下所示:

gl_auth("G:/My Drive/0. Thesis/R-Script/ZakiServiceAccou***************kjadjib****.json")

现在我很开心:)