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/