【问题标题】:Using a token from `googleAuthR` in `googlesheets`在 `google sheet` 中使用来自 `google Auth` 的令牌
【发布时间】:2020-02-02 18:46:06
【问题描述】:

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

这些包中的每一个都有自己独立的 OAuth2.0 进程。我使用googleAuthR 来登录一个闪亮的应用程序,该应用程序又使用googlesheets 库(但使用不同的身份验证过程)。

问题:我们如何将googlesheets 包设置为也使用初始googleAuthR 凭据?

这是我正在使用的过程(在闪亮的应用程序中):

对于我正在使用的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 包,我目前正在使用一个预先注册的(单独的)令牌,该令牌已保存到 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 生成的令牌。

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

我认为googlesheets 的使用可能只适用于短期,并且应该不鼓励,因为谷歌将在 2020 年 3 月 3 日弃用它see message

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

【问题讨论】:

标签: r google-sheets shiny google-authentication


【解决方案1】:

我一直在努力通过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)

【讨论】:

    【解决方案2】:

    您可以尝试使用服务帐户令牌代替身份验证吗? https://gargle.r-lib.org/articles/get-api-credentials.html

    一开始设置可能有点乏味,但对我来说值得

    1. 转到 console.cloud.google.com
    2. 添加项目
    3. 创建凭证并下载 json
    4. 启用 API(谷歌表格)
    5. 复制“新凭据”的地址并转到您要访问/操作(添加用户)的谷歌表格
    library(googlesheets4)
    
    sheets_auth(path = "/path/to/a/service-account.json")
    df <- read_sheet(ss = "sheet_key_here")
    df
    

    【讨论】:

    • 在某些情况下您无法使用 API 密钥(例如访问非公开工作表)。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-07-19
    • 2021-08-20
    • 1970-01-01
    • 2017-04-11
    • 1970-01-01
    • 2019-07-28
    相关资源
    最近更新 更多