8-bit-sheep / googleAnalyticsR

Use the Google Analytics API from R
https://8-bit-sheep.com/googleAnalyticsR/
Other
257 stars 76 forks source link

Run GoogleAnalyticsR in a Docker Container #418

Closed BaronBertil closed 2 weeks ago

BaronBertil commented 1 year ago

What goes wrong

Using this code from this website https://code.markedmondson.me/googleAnalyticsR/articles/shiny.html

and localy it's working fine but when I put it with in a Docker-Container I can log in but then I get the error message

Request Status Code: 403

I don't know whats going wrong here.

Steps to reproduce the problem

app.r

library(shiny)
library(googleAnalyticsR) # for accountPicker
library(googleAuthR)      # for gar_shiny_ui
options(shiny.port = 1221)
gar_set_client(scopes = c("https://www.googleapis.com/auth/analytics.readonly"), activate = "web")
options(googleAuthR.redirect = 'https://*********.a.run.app')
# options(googleAuthR.redirect = 'http://localhost:1221')
options(googleAuthR.verbose = 2)

## ui.R
ui <- fluidPage(
    title = "GA4 Shiny",
    h1("GA4 Shiny App"),
    accountPickerUI("auth_menu", inColumns = TRUE),
    h2("Pick metrics and dimensions"),
    metricDimensionSelectUI("mets1"),
    metricDimensionSelectUI("dims_custom", "Dimensions")
)

## server.R
server <- function(input, output, session){

    token <- gar_shiny_auth(session)

    ga_accounts <- reactive({
        req(token)
        ga_account_list("ga4")
    })

    property_id <- accountPicker("auth_menu",
                                 ga_table = ga_accounts,
                                 id_only = TRUE)

    meta <- reactive({
        req(property_id())

        ga_meta("data", propertyId = property_id())
    })

    metrics <- metricDimensionSelect("mets1", default = "sessions")
    dims_custom <- metricDimensionSelect("dims_custom",
                                         field_type = "dimension",
                                         custom_meta = meta())

}

shinyApp(gar_shiny_ui(ui), server)

.Renviron

GAR_CLIENT_WEB_JSON=./auth/client_secret_551343444456-trpgmtmso8kn17uj1dtg0itl32l6pjsc.apps.googleusercontent.com.json

Dockerfile

FROM rocker/tidyverse:latest

# Install R packages
RUN install2.r --error \
    shiny \
    remotes \
    DT
RUN R -e "remotes::install_github('MarkEdmondson1234/googleAuthR')"
RUN R -e "remotes::install_github('MarkEdmondson1234/googleAnalyticsR')"
# ENTRYPOINT Instruction
COPY . /RedCop
WORKDIR RedCop
run mkdir -p ~/.cache/gargle
ENTRYPOINT ["R", "-e", "shiny::runApp(port = 8080,host = '0.0.0.0')"] # Exec Form

Expected output

App is running

Actual output

Before you run your code, please run:

options(googleAuthR.verbose=2) and copy-paste the console output here.
Check it doesn't include any sensitive info like auth tokens or accountIds - you can usually just edit those out manually and replace with say XXX

'API Data failed to parse' diagnostics

If you have an error starting with:

API Data failed to parse.

remotes::install_github("MarkEdmondson1234/googleAuthR")
googleAuthR::gar_debug_parsing("gar_parse_error.rds")

Session Info

Please run sessionInfo() so we can check what versions of packages you have installed

gronerik commented 2 weeks ago

@BaronBertil, 403 is a permission problem so most likely you have had insufficient permissions to some of the resources (to the GA API perhaps?).

Archiving this for now, as there has been no activity in 2 years regarding this issue. Feel free to re-open if this is still relevant, thanks.