远程服务器身份验证从R脚本不使用服务帐户读取Googlesheets?

时间:2017-07-07 22:53:53

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

我在R中自动化一个拉动,推动,分析和过程的过程。以某种方式存储数据,尤其是Googlesheets,Postgresql和salesforce之间的数据。直到现在,我已经在我的系统中运行它,但我需要在aws上从远程登录ssh服务器运行脚本。

但问题是,我使用的是googlesheets'包装在R到auth&阅读谷歌表,但需要一个交互式环境来注册/设置。我已就此问题阅读了有关SO的其他答案,但他们都建议打开Goog​​le服务帐户。

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

有没有办法从非交互式环境(Linux终端远程登录界面)阅读谷歌表?我可以尝试其他图书馆或任何其他你可能拥有的黑客。

有人吗?

2 个答案:

答案 0 :(得分:5)

我一直在努力寻找类似的东西,所以即使这是一个老问题,我认为其他人也可能会发现此解决方案很有用。我的问题是通过非交互式方式进行身份验证,以通过Shiny App读取和修改Google表格。无论我是否将其保存在应用程序内嵌的缓存文件夹中,该应用程序始终会触发舞蹈验证过程。

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

尝试以下可重复的示例:

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)

答案 1 :(得分:1)

我认为Rstudio的这些文档显示了如何理解,如果我理解你的问题:https://support.rstudio.com/hc/en-us/articles/217952868-Generating-OAuth-tokens-from-a-server