【问题标题】:Connect to googlesheets via shiny in R with googlesheets4使用 googlesheets4 在 R 中通过闪亮连接到 googlesheets
【发布时间】:2020-08-22 10:38:26
【问题描述】:

我正在尝试使用此 example 的更新版本通过闪亮连接到私人 googlesheet,并将此应用程序部署在 shinyapps.io 服务器上。用户无需对 google 帐户进行身份验证,因为该应用使用指定的预先存在的 googlesheet。

我已经关注了这个example(部分复制到这里),试图将令牌保存到我闪亮的应用程序中:

# 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 凭据。您是否在非交互式会话中运行 googlesheets4?...等”我认为令牌将包含足够的信息。

如果有人可以为我提供设置指南,并评论这种方法(在 shinyapps.io 上保存令牌)是否安全,我将不胜感激?

我查看了其他示例,但似乎大多数是针对以前版本的 googlesheets

【问题讨论】:

  • 令牌的生成应该只进行一次。
  • 是的,我生成了一次,然后保存下来,想永久使用它进行身份验证
  • 您可以尝试使用googleAuthR package。我认为闪亮的身份验证尚未在 gargle 中正确实施(googlesheets4 使用)。
  • 谢谢。阅读这个包和漱口的文档,我需要非交互式访问来进行上述使用,似乎推荐的方式是通过服务帐户令牌。我已经尝试过,在google平台上创建一个项目,激活所需的API,并下载服务帐户json,并将其传递给gs4_auth()函数,但仍然没有成功。

标签: r shiny google-sheets-api googlesheets4


【解决方案1】:

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 文件夹中。

【讨论】:

    【解决方案2】:

    只需按照this 链接中的说明进行操作:

    # designate project-specific cache
    options(gargle_oauth_cache = ".secrets")
    # check the value of the option, if you like
    gargle::gargle_oauth_cache()
    # trigger auth on purpose to store a token in the specified cache
    # a broswer will be opened
    googlesheets4::sheets_auth()
    # see your token file in the cache, if you like
    list.files(".secrets/")
    # sheets reauth with specified token and email address
    sheets_auth(
     cache = ".secrets",
     email = "youremail"
     )
    

    【讨论】:

    • 这是在闪亮的服务器上工作。关于此方法与服务帐户的安全性的任何 cmets?例如,在闪亮的服务器上拥有 .secrets 缓存?
    • 知道为什么googlesheets4::sheets_auth() 不再包含在包中了吗?你有什么解决方法吗?
    • @storaged googlesheets4::sheets_auth() 在 v1.0.0 中已弃用。您现在应该改用gs4_auth()。请参阅my answer 了解更多详情。
    猜你喜欢
    • 2020-12-21
    • 2017-10-20
    • 1970-01-01
    • 2014-04-11
    • 1970-01-01
    • 2020-01-18
    • 2020-06-22
    • 2016-09-07
    • 2020-07-31
    相关资源
    最近更新 更多