I am running an R script that reads google sheet for every one hour. I am using googlesheets package and gs_auth() function for this. Initially I am storing the token from running gs_auth() with interactive authentication. From next time, the code just reads that saved token and runs gs_auth(token = ...) function for authentication. Here is the sample code.
## One time execution to save token ##
token <- gs_auth(cache = TRUE)
saveRDS(token, file = "/myfilepath/googlesheets_token.rds")
## reading the saved token everytime the R script runs ##
gs_auth(token = "/myfilepath/googlesheets_token.rds")
This works fine for couple of hours and then gives me this error.
Auto-refreshing stale OAuth token.
Error in function_list[[k]](value) : Unauthorized (HTTP 401).
In addition: Warning message:
Unable to refresh token
Each time this happens, I am running the two line one time code and storing a fresh token, this again runs for couple of hours and then give me same error.
I have also used cache = FALSE
instead of TRUE
, though I don't have clear idea which one to use and its purpose. But that didn't work out.
Also I tried refreshing the token each time it is read from local directory before using in gs_auth()
.
t <- readRDS(file = "/myfilepath/googlesheets_token.rds")
t$refresh()
gs_auth(token = t)
Is there any way that I could resolve this problem and make the authentication work every time without an interactive version.