MarkEdmondson1234 / searchConsoleR

R interface with Google Search Console API v3, including Search Analytics.
http://code.markedmondson.me/searchConsoleR/
Other
114 stars 41 forks source link

with_shiny not giving authentication credentials to search_analytics() #61

Closed domsle closed 3 years ago

domsle commented 3 years ago

What goes wrong

When requesting search_analytics() using with_shiny() in a authenticated shiny app, the function does not recognise existing authentication, and throws this error:

Warning: Error in : Not authenticated. Run scr_auth()

Steps to reproduce the problem

app.R:

library(shiny)
library(searchConsoleR)
library(googleAuthR)
library(dplyr)

options(googleAuthR.verbose = 2)
gar_set_client(web_json = "client.json",
               scopes = "https://www.googleapis.com/auth/webmasters")

ui <- fluidPage(
  googleAuth_jsUI('auth', login_text = 'Login to Google'),
  tableOutput("sc_accounts"),
  tableOutput("sc_data")
)

server <- function(input, output, session) {

  this_url <- reactive({
    paste0(
      session$clientData$url_protocol, "//", session$clientData$url_hostname, ifelse(session$clientData$url_port == "" | is.null(session$clientData$url_port), "", ":"), session$clientData$url_port,
      session$clientData$url_pathname
    )
  })

  observeEvent(session$clientData$url_protocol,
               {options(googleAuthR.redirect = this_url())
               observe(print(getOption('googleAuthR.redirect')))}
               )
  auth <- callModule(googleAuth_js, "auth")

  sc_accounts <- reactive({
    req(auth())

    with_shiny(  # This works just fine
      list_websites,
      shiny_access_token = auth()
    )

  })

  output$sc_accounts <- renderTable({
    sc_accounts()
  })

  sc_data <- reactive({
    req(auth())

    with_shiny( # This one throws error
      search_analytics,
      siteURL = 'http://example.com/',
      startDate = as.Date("2020-06-01", origin="1970-01-01"),
      endDate = as.Date("2020-06-07", origin="1970-01-01"),
      dimensions = c("date"),
      searchType = "web",
      rowLimit = 200,
      dimensionFilterExp = NULL,
      shiny_access_token = auth()
    )

  })
  output$sc_data <- renderTable({
    sc_data()
  })

}

shinyApp(ui = ui, server = server)

Expected output

Expectedly this would show me both list of websites, and any output from search_analytics.

Actual output

Running this chunk:

with_shiny(
      search_analytics, ...

Caused this error to occur: image

Warning: Error in : Not authenticated. Run scr_auth()
  116: stop
  115: f
  114: with_shiny
  113: <reactive:sc_data> [/root/app/app.R#50]
   97: sc_data
   96: renderTable [/root/app/app.R#64]
   95: func
   82: origRenderFunc
   81: output$sc_data
    1: shiny::runApp
ℹ 2020-07-30 13:35:33 > Request:  https://www.googleapis.com/webmasters/v3/sites/

options(googleAuthR.verbose=2) :

ℹ 2020-07-30 13:42:29 >
options(googleAuthR.scopes.selected=c(' https://www.googleapis.com/auth/webmasters '))
options(googleAuthR.client_id='  ')
options(googleAuthR.client_secret='   ')
options(googleAuthR.webapp.client_id='XXX.apps.googleusercontent.com ')
options(googleAuthR.webapp.client_secret='YYYYY')

Session Info

R version 4.0.2 (2020-06-22)
Platform: x86_64-pc-linux-gnu (64-bit)
Running under: Ubuntu 20.04 LTS

Matrix products: default
BLAS/LAPACK: /usr/lib/x86_64-linux-gnu/openblas-openmp/libopenblasp-r0.3.8.so

locale:
 [1] LC_CTYPE=en_US.UTF-8       LC_NUMERIC=C
 [3] LC_TIME=en_US.UTF-8        LC_COLLATE=en_US.UTF-8
 [5] LC_MONETARY=en_US.UTF-8    LC_MESSAGES=C
 [7] LC_PAPER=en_US.UTF-8       LC_NAME=C
 [9] LC_ADDRESS=C               LC_TELEPHONE=C
[11] LC_MEASUREMENT=en_US.UTF-8 LC_IDENTIFICATION=C

attached base packages:
[1] stats     graphics  grDevices utils     datasets  methods   base

other attached packages:
[1] dplyr_1.0.0          googleAuthR_1.3.0    searchConsoleR_0.4.0
[4] shiny_1.5.0

loaded via a namespace (and not attached):
 [1] Rcpp_1.0.5       magrittr_1.5     tidyselect_1.1.0 xtable_1.8-4
 [5] R6_2.4.1         rlang_0.4.7      fastmap_1.0.1    fansi_0.4.1
 [9] httr_1.4.2       tools_4.0.2      cli_2.0.2        withr_2.2.0
[13] ellipsis_0.3.1   htmltools_0.5.0  digest_0.6.25    assertthat_0.2.1
[17] tibble_3.0.3     lifecycle_0.2.0  gargle_0.5.0     crayon_1.3.4
[21] purrr_0.3.4      later_1.1.0.1    vctrs_0.3.2      promises_1.1.1
[25] fs_1.4.2         memoise_1.1.0    glue_1.4.1       mime_0.9
[29] pillar_1.4.6     compiler_4.0.2   generics_0.0.2   jsonlite_1.7.0
[33] httpuv_1.5.4     pkgconfig_2.0.3

Thank you for your help in advance!

MarkEdmondson1234 commented 3 years ago

This is due to with_shiny() actually beginning to be deprecated in favour of gar_shiny_* functions, I should update documentation to help, but you can work around it via this, which will add the token to the environment checked by search_analytics():

  sc_data <- reactive({
    req(auth())

    with_shiny( 
      search_analytics,
      siteURL = 'https://code.markedmondson.me/',
      startDate = as.Date("2020-06-01"),
      endDate = as.Date("2020-06-07"),
      dimensions = c("date"),
      searchType = "web",
      rowLimit = 200,
      dimensionFilterExp = NULL,
      shiny_access_token = scr_auth(auth()) # add the reactive token to the googleAuthR auth environment
    )

  })