5

我正在运行一个 R 脚本,它每隔一小时读取一次谷歌表。我为此使用 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")

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

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

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

t <- readRDS(file = "/myfilepath/googlesheets_token.rds")
t$refresh()
gs_auth(token = t)

有什么方法可以解决这个问题,并且每次都可以在没有交互式版本的情况下进行身份验证。

4

0 回答 0