1

I'm trying to automatically run an r script to download a private Google Sheet every hour. It always works fine when I'm interactively using R. It also works fine during the first hour after I automate the script with launchd.

It stops working an hour after I start automating it with launchd. I think the problem is that after one hour the access token changes, and the non-interactive version isn’t waiting for the auto refreshing of the OAuth token. Here is the error that I get from the error report:

Auto-refreshing stale OAuth token. Error in gzfile(file, mode) : cannot open the connection Calls: gs_auth ... -> -> cache_token -> saveRDS -> gzfile In addition: Warning message: In gzfile(file, mode) : cannot open compressed file '.httr-oauth', probable reason 'Permission denied' Execution halted

I'm using Jenny Bryan's googlesheets package. Here is the code that I initially use to register the sheet, and then save the oAuth token:

gToken <- gs_auth() # Run this the first time to get the oAuth information
saveRDS(gToken, "/Users/…/gToken.rds") # Save the oAuth information for non-interactive use

I then use the following script in the file that I automate with launchd:

gs_auth(token = "/Users/…/gToken.rds")

How can I avoid this error when running the script automatically with launchd?

xgord
  • 4,606
  • 6
  • 30
  • 51
Ron Guymon
  • 11
  • 3

1 Answers1

0

I don't know about launchd but I had the same problem when I wanted to run a R script automatically from the Windows task planer. Changing the 'cache' attribute value to FALSE did the trick for me [1]: https://i.stack.imgur.com/pprlC.png

You can find the solution here: https://github.com/jennybc/googlesheets/issues/262

To authenticate once in the browser in order to get a token file, I did this:

token_file <- gs_auth(new_user = TRUE, cache = FALSE)
saveRDS(token_file, "googlesheets_token.rds")

Automatic login afterwards via:

gs_auth(token = paste0(path_scripts, "googlesheets_token.rds"), 
verbose = TRUE, cache = FALSE)