r - 使用 json 路径对 googlesheets4 进行非交互式身份验证

标签 r google-sheets google-sheets-api googlesheets4

虽然它不是我的主要语言,但我正在使用 R 将每日 ETL 的结果发布到 Google 表格工作表中。因为这将是一个永久运行的计划作业,所以我对使用交互式流程从 R 验证 Google Drive 犹豫不决。我从我的 Google Drive 帐户凭据中获得了我的 JSON 的路径,但是当我通过

gs4_deauth()
gs4_has_token()
gs4_auth(email=<email-string>, path = jsonlite::fromJSON(<path-to-json>), cache = NULL)

它抛出以下错误:

[1] FALSE
Error: Can't get Google credentials.
Are you running googlesheets4 in a non-interactive session? Consider:
  * `gs4_deauth()` to prevent the attempt to get credentials.
  * Call `gs4_auth()` directly with all necessary specifics.
See gargle's "Non-interactive auth" vignette for more details:
https://gargle.r-lib.org/articles/non-interactive-auth.html
Execution halted

由于错误消息没有提供太多细节,我想知道是否有人对此有经验或知道为什么会发生这种情况?我正在使用 gs4_deauth(),因为我最初是使用交互式流程登录的,但现在想确保非交互式身份验证有效。

顺便说一句,从 json 凭据文件创建 token 对象最简单的方法是什么?我相信这可能比直接使用 path 参数重复传递 json 凭据更简单。

最佳答案

如果是服务帐户凭据:

googlesheets4::gs4_auth(
  path = "service_accout.json",
  scopes = "https://www.googleapis.com/auth/spreadsheets.readonly"
)

## NOTE: the spreadsheet is shared with the service account
s <- googlesheets4::read_sheet(ID_OF_YOUR_SPREADSHEET)

token 可以通过这种方式重复使用:

token <- googlesheets4::gs4_token()
googledrive::drive_auth(token = token)
googledrive::drive_has_token()
[1] TRUE

关于r - 使用 json 路径对 googlesheets4 进行非交互式身份验证,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63289979/

相关文章:

r - 将数据框中的列拆分为 R 中的列表

google-sheets - 如何在 Google 表格中拆分时间戳

google-sheets - 如何通过 Google Sheets API 保护工作表?

java - Google Java api - feed.insert(entry) 期间出现错误 "com.google.gdata.util.ServiceException: Bad Gateway"

javascript - 即使我正在替换 URL,仍出现错误 "Data column(s) for axis #0 cannot be of type string"

javascript - 使用公共(public) API 访问(无 OAuth)从 JavaScript 写入 Google 电子表格

r - 在 R 中绘制 map - 仅显示外部边界

r - 如何在 R 中绘制具有 3 个因子的响应变量?

r - 如何使用 maptools、ggplot 或其他软件包在 R map 中添加城市和质心的名称?

google-sheets - 如何在 Google Sheets 中使用带有多个 IF 条件的 ArrayFormula?