使用 googlesheets4 通过 Shiny app 授权非交互式使用 googlesheets

Authorizing non-interactive use of googlesheets through Shiny app using googlesheets4

我知道还有其他一些类似的问题围绕着 SO(例如,here and ),但我想我会再试一次,因为其他人没有得到很多答案。

我有一个 Shiny 应用程序,允许其用户通过该应用程序提供输入,但这些用户需要跳过身份验证步骤。在之前的 googlesheets 版本中,我设置了 sheet public(对任何拥有 link 的人)并避免了一些身份验证过程。在更新到 googlesheets4 时,我 运行 遇到了将更新后的应用程序部署到 shinyapps.io 的权限问题。这是我尝试过的内容的总结,以及我得到的结果。任何指针将不胜感激。

首先,根据 this gargle article 中的建议,我创建了一个服务帐户,下载了一个 JSON 服务帐户令牌(格式为“project-name-12345678abc1.json”),然后已将其(暂时)保存在父应用程序文件夹中,假设它需要与应用程序包一起上传。然后我启用了 Google Sheets API.

我已将以下命令放在应用程序的前面,在任何实质内容之前:

  1. gs4_deauth(),因为 google sheet 对于任何拥有 link 的人来说都是 public,因此可能不需要令牌.

  2. Following this article,gs4_auth(path = "project-name-12345678abc1.json")。我还添加了 scopes 参数(如下所示)和 use_oob=TRUE.

  3. credentials_service_account(scopes = "https://www.googleapis.com/auth/spreadsheets", path = "project-name-12345678abc1.json").

  4. credentials_app_default(path = "project-name-12345678abc1.json")

结果

大多数组合都会给我错误提示“无法获得 Google 凭据”,然后建议我查看漱口网站上的非交互式文章。一个例外是尝试 3,它提供了以下错误消息 - 奇怪的是:

Warning: Error in : Client error: (403) PERMISSION_DENIED

  • Client does not have sufficient permission. This can happen because the OAuth token does not have the right scopes, the client doesn't have permission, or the API has not been enabled for the client project.

关于我可能遗漏的任何想法?该应用程序在本地运行良好。

感谢任何帮助。谢谢!

已使用 this (closed) Github issue 中确定的方法解决了此问题。

关键是要完全按照以下步骤操作:

  1. 在浏览器中本地调用 gs4_auth(cache = ".secrets") 一次 ,运行 应用程序。
  2. 将上面的行替换为 gs4_auth(cache = ".secrets", email = TRUE, use_oob = TRUE)
  3. 将应用程序部署到 shinyapps。

我还清除了之前保存的标记,这可能很重要,但很难知道。

如 Github 讨论中所述,可能没有必要包含 use_oob = TRUE。此外,漱口水网站上的 non-interactive auth 文章详细介绍了这种方法。