【问题标题】:Remote server authentication to read Googlesheets from R script not using service accounts?远程服务器身份验证从 R 脚本读取 Googlesheets 不使用服务帐户?
【发布时间】:2017-12-12 08:11:09
【问题描述】:

我正在自动化 R 中的流程,该流程以某种方式提取、推送、分析和存储数据,总而言之,在 Googlesheets、Postgresql 和 salesforce 之间处理数据。到目前为止,我已经在我的系统中运行它,但我需要从我的远程登录 ssh 服务器上运行脚本。

问题是,我在 R 中使用“googlesheets”包来验证和阅读 google 表格,但这需要一个交互式环境来注册/设置。我已经阅读了关于这个问题的其他答案,但他们都建议开设一个谷歌服务帐户。

但是,我不想这样做,因为我只需要阅读那些本身是免费的谷歌表格。我现在没有 GCP、谷歌计算、bigquery 或任何这些,我现在肯定无法获得任何报酬。

有没有办法从非交互环境(linux终端远程登录界面)读取google sheet?我愿意尝试其他库或您可能拥有的任何其他 hack。

有什么事吗?

【问题讨论】:

  • 你有没有想过这个问题?我有完全一样的问题。需要以某种方式在服务器上进行自动身份验证。
  • 是的,我做了,你需要创建一个谷歌服务帐户,不用担心你可以免费做(但有一些限制,但我没有完全理解那部分).. 一旦你做了那,使用 gspread 库和谷歌,你会得到它。 DW,你有这个!

标签: r remote-access interactive google-sheets-api r-googlesheets


【解决方案1】:

我已经为类似的事情苦苦挣扎了好几个小时,所以即使这是一个老问题,我认为其他一些人可能会发现这个解决方案很有用。我的问题是通过 Shiny App 以非交互方式读取和修改谷歌表格的身份验证。无论我是否将其保存在应用程序中嵌入的缓存文件夹中,该应用程序都会始终触发舞蹈身份验证过程。

我使用以下指南和问题来指导自己完成整个过程:

尝试以下可重现的示例:

library("googledrive")
library("googlesheets4") # I am using the developing version 0.1.0.9000
library("shiny")

# You want to deploy an app in Shinyapps.io or other server
# FIRST STEP----
# Get the token an store it in a cache folder embedded in your app directory
# designate project-specific cache
options(gargle_oauth_cache = ".secrets")
# options(gargle_quiet = FALSE) # So you can know what is happening
# Authenticate in interactive mode (run the app) ONCE and check if the token 
# has been stored inside the .secrets folder, after that just comment this line
#drive_auth() # Authenticate to produce the token in the cache folder
# Grant permission to googlesheets to access to the token produced
#sheets_auth(token = drive_token())

# SECOND STEP----
# Comment lines 10, 13 and 15 and uncomment lines 21 and 22
# You tell gargle to search the token in the secrets folder and to look
# for an auth given to a certain email (enter your email linked to googledrive!)
drive_auth(cache = ".secrets", email = "enter_your_email@here")
sheets_auth(token = drive_token())

# THIRD STEP---
# Now you can deploy your app in shinyapps.io!!
# Test if your app runs properly in the local version
# Authenticate in ShinyApps.io
# rsconnect::setAccountInfo(name="<ACCOUNT>", token="<TOKEN>", secret="<SECRET>")
# setwd() in your App directory
# library(rsconnect)
# deployApp()
# Enjoy your new App!!

ui <- # Define UI for application that plots random distributions 
  fluidPage(

    # Application title
    titlePanel("Hello Shiny!"),

    # Sidebar with a slider input for number of observations
    sidebarLayout(
      sidebarPanel(
        sliderInput("obs", 
                    "Number of observations:", 
                    min = 1, 
                    max = 1000, 
                    value = 500),
        actionButton(
          "add",
          "Add new entry")
      ),

      # Show a plot of the generated distribution
      mainPanel(
        "Check your googlesheet!!"
      )
    )
  )

server <- function(input, output, session) {
  # Expression that generates a plot of the distribution. The expression
  # is wrapped in a call to renderPlot to indicate that:
  #
  #  1) It is "reactive" and therefore should be automatically 
  #     re-executed when inputs change
  #  2) Its output type is a plot 
  #
  observeEvent(input$add, {
    # You should have or create a googlesheets through google drive with
    # the name "example_shiny"
    wb <- drive_get("example_shiny")
    dt <- read_sheet(wb)
    new_entry <-
      data.frame(ID = tail(dt$ID, 1) + 1, NAME = "new",
                 OBSERVATION = input$obs)
    sheets_append(new_entry, wb)
  })
}

shinyApp(ui, server)

【讨论】:

    【解决方案2】:

    如果我理解您的问题,我认为 Rstudio 的这段文档说明了如何做到这一点:https://support.rstudio.com/hc/en-us/articles/217952868-Generating-OAuth-tokens-from-a-server

    【讨论】:

      猜你喜欢
      • 2021-04-27
      • 2020-11-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-02-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多