本文介绍了远程服务器身份验证可不使用服务帐户从R脚本读取Googlesheets?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在R中自动进行拉,推,分析和处理的过程.以某种方式存储数据,总而言之,在Googlesheets,Postgresql和Salesforce之间处理数据.到现在为止,我已经在系统中运行它了,但是我需要从aws上的远程登录ssh服务器运行脚本.

I'm automating a process in R that pulls, pushes, analyses & stores data in a certain way and all in all, juggles data between Googlesheets, Postgresql, and salesforce. Till now, I had it running in my system but I will need to run the scripts from my remote login ssh server on aws.

问题是,我在R中使用"googlesheets"包来验证&阅读Google表格,但需要一个交互式的环境来注册/设置.我已经阅读了有关此问题的其他答案,但他们都建议开设一个Google服务帐户.

The thing is though, I use 'googlesheets' package in R to auth & read google sheets but that needs an interactive environment to register/setup. I've read other answers on SO about this issue but they all suggest opening a google service account.

但是,我不想这样做,因为我所需要的只是阅读那些本身是免费的google表格.我现在没有GCP,谷歌计算,bigquery或任何此类工具,我现在当然无法获得任何报酬.

However, I dont want to do this because all I need is to read those google sheets which by itself is free. I have no GCP, google compute, bigquery or any of those right now and I certainly cant get pay for anything now.

有没有办法从非交互式环境(Linux终端远程登录界面)中读取Google表格?我愿意尝试其他库或您可能拥有的其他任何破解方法.

Is there a way to read google sheets from a non interactive environment (linux terminal remote login interface)? I'm open to trying other libraries or any other hack that you may have.

有什么人吗?

推荐答案

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

I was struggling with something similar for many hours, so even if this an old question, I think some other people might find this solution useful. My problem was the authentication in a non interactive way to read and modify a google sheet through a Shiny App. The app use to always trigger the dance auth process no matter if I would have saved it in a cache folder embedded in the app.

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

I have used the following guides and questions to guide myself through the process:

  • https://cran.r-project.org/web/packages/gargle/vignettes/non-interactive-auth.html
  • https://community.rstudio.com/t/shinyapps-io-non-interactive-auth-fails-for-googledrive/46504
  • https://docs.rstudio.com/shinyapps.io/getting-started.html#deploying-applications

尝试以下可重复的示例:

Try the following reproducible example:

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)

这篇关于远程服务器身份验证可不使用服务帐户从R脚本读取Googlesheets?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-20 21:50
查看更多