R Googlesheets软件包和gs_auth()函数-非交互式自动刷新陈旧令牌问题

R B*_*Bud 5 r rscript r-googlesheets google-sheets-api

我正在运行一个R脚本,该脚本每隔一小时读取一次Google表格。我为此使用googlesheets软件包和gs_auth()函数。最初,我通过交互式身份验证运行gs_auth()来存储令牌。从下一次开始,代码将仅读取保存的令牌并运行gs_auth(token = ...)函数进行身份验证。这是示例代码。

## 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")
Run Code Online (Sandbox Code Playgroud)

这可以正常工作几个小时,然后给我这个错误。

Auto-refreshing stale OAuth token.
Error in function_list[[k]](value) : Unauthorized (HTTP 401).
In addition: Warning message:
Unable to refresh token 
Run Code Online (Sandbox Code Playgroud)

每次发生这种情况时,我都会运行两行一次的代码并存储一个新令牌,这又会运行几个小时,然后出现相同的错误。我也使用cache = FALSE代替TRUE,尽管我不清楚要使用哪个及其用途。但这并没有解决。我也尝试在每次使用in之前从本地目录读取令牌时刷新令牌gs_auth()

t <- readRDS(file = "/myfilepath/googlesheets_token.rds")
t$refresh()
gs_auth(token = t)
Run Code Online (Sandbox Code Playgroud)

有没有什么方法可以解决此问题,并使每次身份验证都能正常工作而无需交互版本。