1

I am currently working on analyzing twitter data , however when I attempt to translate twitter text data through the translate() package leveraging the use of Google API in R I get this error.

 t <- gl_translate(my_data$text, source="ar", target="en", )
2020-07-19 11:25:30 -- Translating text: 69597 characters - 
ℹ 

2020-07-19 11:25:32 > Request Status Code:  400

2020-07-19 11:25:32 -- API returned: Request payload size exceeds the limit: 204800 bytes.
2020-07-19 11:25:32 -- Attempting to split into several API calls
2020-07-19 11:25:32 -- Translating text: 98 characters - 
Auto-refreshing stale OAuth token.
ℹ 2020-07-19 11:25:33 > Request Status Code:  403
Error: API returned: Request had insufficient authentication scopes.

why does it produce this error?

this is my code:

gar_auth(email="**@**.com")
set.key('****')
t <- gl_translate(my_data$text, source="ar", target="en", )
View(t)
  • Please read [(1)](https://stackoverflow.com/help/how-to-ask) how do I ask a good question, [(2)](https://stackoverflow.com/help/mcve) how to create a MCVE as well as [(3)](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example#answer-5963610) how to provide a minimal reproducible example in R. Then edit and improve your question accordingly. I.e., abstract from your real problem... – Christoph Jul 16 '20 at 12:57

1 Answers1

1

If you use googleLanguageR package (it is not clear from the question itself), it seems like an authentication issue.

You'll probably need to authenticate with:

library(googleLanguageR)
gl_auth("translate_api_key.json")

To get the JSON authentication file, see Google Cloud Translation - Creating service accounts and keys.

For details on authenticating the googleLanguageR package in R, see Language tools for R via Google Machine Learning APIs - Authentication.

Note that Google API has some pricing for this.

If you want to use a Google Translate API key rather than JSON file, there are:

  • translate package, where you store you API key in getOption("google.key") variable, or you pass a key argument with you API key, and
  • github/sumtxt/datatools package with gl_translate() function, where you can provide the API key in the key argument as well,

but note that you have to generate you API key in Google Cloud Console as well (not to be confused with a password of your Google account).

  • Thank you Petr for your answer, I am using the transate() package once I performed what you suggested it translates the first line in my text, is there any possible way to make it translate all text data I have? – Fatemah Alnofal Jul 19 '20 at 10:40
  • @FatemahAlnofal Then it's probably not vectorized, i.e. you need to provide only one string at a time. You can use `dplyr` package for this or you can try to vectorize that function with `my_translate <- Vectorize(gl_translate)` and then use `t <- my_translate(my_data$text, source="ar", target="en")`. Let me know if that works. –  Jul 19 '20 at 10:59
  • @PetrKazjar I have vectorized the data yet returns only one row of translation – Fatemah Alnofal Jul 19 '20 at 11:19
  • @FatemahAlnofal OK, then please let me know exactly your package... package [translate](https://cran.r-project.org/package=translate) doesn't seem to have function `gl_translate()`, so you maybe use something else? –  Jul 19 '20 at 11:26
  • yes, I changed to the translate package due to authentication issues. this is my code getOption("google.key") gar_auth(email="fatima@ekadkom.com") set.key('**') my_translate <- Vectorize(translate) t <- my_translate(my_data$text, source="ar", target="en", ) } – Fatemah Alnofal Jul 19 '20 at 11:44
  • @FatemahAlnofal Yes, thanks! Then try to run `t <- sapply(my_data$text, translate, source = "ar", target = "en")`. –  Jul 19 '20 at 11:57
  • Still only translate the first line of my data. – Fatemah Alnofal Jul 19 '20 at 13:27
  • @FatemahAlnofal That's strange. So resolve this question (which is basically about authentication) and open a new question with this new problem, please, there will be certainly someone who could help with this. –  Jul 20 '20 at 06:57