我正在尝试使用此 example 的更新版本通过 Shiny 连接到私有(private) googlesheet,并将此应用程序部署在 shinyapps.io 服务器上。由于应用程序使用指定的预先存在的 googlesheet,因此用户无需对 google 帐户进行身份验证。
我关注了这个example (部分复制在这里),试图将 token 保存到我 Shiny 的应用程序:
# previous googlesheets package version:
shiny_token <- gs_auth() # authenticate w/ your desired Google identity here
saveRDS(shiny_token, "shiny_app_token.rds")
但尝试将其更新为 googlesheets4,如下所示:ss <- gs4_get("MY GOOGLE DOC URL") # do the authentication once, manually.
ss
gs4_has_token() # check that the token exists
# get token
ss_token <- gs4_token()
# save the token
save(ss_token, file = "APP PATH ... /data/tk.rdata")
然后在应用程序中,我将此代码放在 shinyApp()
之外功能。load("data/tk.rdata")
googlesheets4::gs4_auth(token = ss_token, use_oob = T)
在应用程序中,我使用从应用程序获得的硬编码 id 从应用程序连接到谷歌文档ss$spreadsheet_id
多于。该应用程序在本地运行。尝试将应用程序部署到服务器后,我收到错误“...无法获取 google 凭据。您是否在非交互式 session 中运行 googlesheets4?...等”我认为 token 将包含足够的信息这。
如果有人能指出我的设置指南,并评论这种方法(在 shinyapps.io 上保存 token )是否安全,我将不胜感激?
我查看了其他示例,但似乎大多数是针对
googlesheets
的早期版本的。
最佳答案
2021 年 7 月 21 日 googlesheets4 deprecated some of its function when releasing v1.0.0 .
我更新了volfi's answer使用 googlesheets4 v1.0.0。
它在部署到 shinyapps.io 时也有效。
设置非交互式身份验证
library(googlesheets4)
# Set authentication token to be stored in a folder called `.secrets`
options(gargle_oauth_cache = ".secrets")
# Authenticate manually
gs4_auth()
# If successful, the previous step stores a token file.
# Check that a file has been created with:
list.files(".secrets/")
# Check that the non-interactive authentication works by first deauthorizing:
gs4_deauth()
# Authenticate using token. If no browser opens, the authentication works.
gs4_auth(cache = ".secrets", email = "your@email.com")
示例 - 将数据添加到 Google 表格在 Google Sheets 上创建 Google 表格并复制工作表的网址。
library(googlesheets4)
gs4_auth(cache=".secrets", email="your@email.com")
ss <- gs4_get("https://docs.google.com/path/to/your/sheet")
sheet_append(ss, data.frame(time=Sys.time()))
如果将您的应用程序部署到 shinyapps.io,请确保将文件部署到 .secrets
文件夹。
关于r - 使用 googlesheets4 通过 R 中的 Shiny 连接到 googlesheets,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63535190/