r - 在 `googleAuthR` 中使用来自 `googlesheets` 的 token

标签 r google-sheets shiny google-authentication

googleAuthR是一个 R 包,它包装了 google API 客户端库(API 的身份验证和进一步使用)。 googlesheets R 包用于与 google 表格 API 集成(即,围绕 google 表格 API 的包装器)。

这些包中的每一个都有自己独立的 OAuth2.0 进程。我正在使用 googleAuthR为了登录一个 Shiny 的应用程序,该应用程序反过来也使用 googlesheets库(但具有不同的身份验证过程)。

问题:我们如何设置googlesheets包使用初始googleAuthR还有凭据?

这是我正在使用的过程(在一个 Shiny 的应用程序中):

对于 googleAuthR我正在使用的登录:

options(googleAuthR.webapp.client_secret = "***REMOVED_FROM_EXAMPLE***")
options(googleAuthR.webapp.client_id = "***REMOVED_FROM_EXAMPLE***")
options(googleAuthR.scopes.selected = c("https://www.googleapis.com/auth/userinfo.email",
                                        "https://www.googleapis.com/auth/userinfo.profile",
                                        "https://www.googleapis.com/auth/spreadsheets"))


而对于 googlesheets包,我目前正在使用预先注册的(单独的) token ,我将其保存到 RDS 文件中,如包的小插图中所示:
suppressMessages(gs_auth(token = "googlesheets_token.rds", verbose = FALSE))
gsheet_log <- googlesheets::gs_url("https://docs.google.com/spreadsheets/d/***REMOVED_FROM_EXAMPLE***/edit#gid=0")

我想要一个流程来代替 gs_auth 的使用使用 googleAuthR 生成的 token .

重要提示 (稍后添加到此问题中):

我想也许是使用googlesheets仅适用于短期和 应该劝阻 ,因为谷歌将在 2020 年 3 月 3 日弃用它 see message

因此@Aurèle 的评论可能是通往这里的方式(希望 googlesheets4 最终将获得其前身的所有功能)。从更广泛的意义上说,这个问题可能仍然很有趣。

最佳答案

我一直在努力通过 googleAuthR 进行身份验证然后使用我的凭据使用 googlesheets4 阅读工作表图书馆。不幸的是,我仍然无法使它工作,但我在“纯”googleAuthR 中找到了一个粗略的解决方法。 .使用 gar_api_generator您实际上可以调用 Google API 中允许的任何请求。

library(shiny)
library(googleAuthR)
options(shiny.port = 8787)
options(googleAuthR.redirect = "http://localhost:8787")

# JSON with you client data from GCP
gar_set_client(scopes = "https://www.googleapis.com/auth/spreadsheets.readonly",
               web_json = "<YOUR_JSON>")
spreadsheet_key <- "<YOUR SHEET>"

read_googlesheet <- gar_api_generator(
  baseURI = "https://sheets.googleapis.com/v4/",
  http_header = 'GET',
  path_args = list(spreadsheets = spreadsheet_key,
                   values = "A:U"), #column range 
  data_parse_function = function(x) x$values
)

## ui.R
ui <- fluidPage(title = "googleAuthR Shiny Demo",
                tableOutput("gs")
)

## server.R
server <- function(input, output, session){
  gar_shiny_auth(session)

  output$gs <- renderTable({
    df_raw <- read_googlesheet()
    # make the first row of the dataset as a header
    df <- df_raw[c(2:nrow(df_raw)), ]
    colnames(df) <- df_raw[1, ]
    df
  })
}

shinyApp(gar_shiny_ui(ui, login_ui = gar_shiny_login_ui), server)

关于r - 在 `googleAuthR` 中使用来自 `googlesheets` 的 token ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58236227/

相关文章:

r - 将 S4 对象存储在矩阵中

r - 在2个日期之间对数据框进行分组

r - 如何在 R 热图中为连续颜色条设置特定颜色?

javascript - 如何使用脚本编辑器在谷歌表格中选择整行并以不同颜色突出显示?

javascript - 当仅绘制某些观察值时,框选择时返回不正确的行索引 - Plotly、htmlWidgets、Shiny

r - 如何在 R Shiny 的 navbarPage 布局中移动导航选项卡?

r - 如何在 SparkR 中建立逻辑回归模型

javascript - 对数组中的范围进行排序

html - 传输或处理此请求时出错。错误代码 = 10,路径 =/wardeninit

r - 使用R中Shiny中的renderPlot调整要在绘图标题中显示的小p值的位数