Shiny Dashboard 中 Google Analytics API 的身份验证范围问题

Authentification Scope Problems with Google Analytics API in Shiny Dashboard

我正在设置一个闪亮的仪表板,以通过 Google 分析报告 API 获取非抽样报告。这是我的第一个 Shiny-Projekt,所以也许我的问题的解决方案非常简单。不幸的是,我找不到任何可以帮助我的东西。欢迎来到我关于 Whosebug 的第一个问题 :)。

我已经将身份验证范围设置为最高,并在自己的项目中设置了客户端 web id。基本上所有已找到的教程中最佳实践的所有内容(非常尊重 Mark Edmondson!)。

    library(shiny) # R webapps
    library(googleAuthR) # auth login

# refresh authenticiaction token and set client scope

gar_set_client(web_json = "CLIENTID-JSONFILE", scopes = ("https://www.googleapis.com/auth/analytics"))

library(googleAnalyticsR) 

####################
# Shiny: USER INTERFACE
####################

# Define UI
ui <- fluidPage(

  # Authorization Login-Button
  googleAuth_jsUI("auth", login_text = "Log In"),   

  # Drop-Down Menue: Account, Property, View
  column(width=12, authDropdownUI("auth_dropdown", inColumns = FALSE)), # Modul Auswahl des Views 1 von 2
  dateRangeInput("datepicker", NULL, start = Sys.Date()-30),

  # The dimension selector (dropdown) 
  selectInput("dim", label = "Please select at least one dimension", 
              choices = dimension_options, 
              selected = c("date","hour","minute"),
              multiple = TRUE),

  # The metric dropdown
  selectInput("metric", label = "Please select at least one and maximal ten metrics", 
              choices = metric_options, 
              selected = "sessions",
              multiple = TRUE),

  # Download Button 
  downloadButton("downloadData", "Download")
                 )




####################
# Shiny: SERVER LOGIK
####################

# Define server logic
server <- function(input, output, session) {

  # get authorizatin token
  auth <- callModule(googleAuth_js,"auth") 

  # Accountliste: 
  ga_accounts <- reactive({
    req(auth())
    with_shiny(
      ga_account_list,
      shiny_access_token = auth())
    })

  # Views: Greift auf die Accountliste zu
  view_id <- callModule(authDropdown, "auth_dropdown",
                        ga.table = ga_accounts)

  # Daten abrufen
  ga_data <- reactive({
    req(view_id())
    req(input$datepicker)
    req(input$dim)
    req(input$metric)

    with_shiny(
      google_analytics,
      view_id(),
      date_range <- input$datepicker,
      metrics <- input$metric, 
      dimensions <- input$dim,      
      max = -1, #kein Sampling
      shiny_access_token = auth() 
    )
  })

  # Daten downloaden
  output$downloadData <- downloadHandler(
    filename = function() {
      paste("ViewID_",view_id(), ".csv", sep="")
    },
    content = function(file){
      write.csv2(ga_data(), file, row.names = FALSE)
    })


}

# Run the application 
shinyApp(ui = ui, server = server)

问题是,尽管代码在几天前有效,但我还是收到了以下错误代码:

Warning: Error in : API returned: Insufficient Permission: Request had insufficient authentication scopes.

  93: stop
  92: checkGoogleAPIError
  91: _f
  89: cachedHttrRequest
  88: memDoHttrRequest
  87: f
  86: gar_api_page
  85: f
  84: with_shiny
  83: <reactive:ga_accounts> [S:/GA_Report_Shiny/shinyapp_v0.R]
  67: ga.table
  66: <reactive>
  50: pList
  44: <observer>
   1: runApp

您需要“https://www.googleapis.com/auth/analytics.edit”来列出 GA 帐户,这是上面 ga.table 功能所需要的。

您需要了解的第一个 Shiny-Project Google 分析报告 API。请参阅下文,我提到 API 控制台支持两种类型的凭据。 OAuth 2.0 和 API 密钥。

    // Create the service.
            var service = new DiscoveryService(new BaseClientService.Initializer
                {
                    ApplicationName = "Discovery Sample",
                    ApiKey="[YOUR_API_KEY_HERE]",
                });

// Run the request.
            Console.WriteLine("Executing a list request...");
            var result = await service.Apis.List().ExecuteAsync();

            // Display the results.
            if (result.Items != null)
            {
                foreach (DirectoryList.ItemsData api in result.Items)
                {
                    Console.WriteLine(api.Id + " - " + api.Title);
                }
            }

您还可以参考them以获得更完整的解决方案。