4

I am working on building a Shiny web app in R that uses Google APIs. I rely heavily on {googledrive}, {googlesheets4}, and {gargle}. I've also done some experimentation with {googleAuthR} but have not been successful at getting sign on and authentication to work on shinyapps.io.

The app will require a user to log in to Google. It will then ask a few questions and then create a handful of files based on the information given. I have registered an OAuth2 client through Google. I also tried my best to follow the information on non-interactive auth given by the developers of the package.

The app works fantastic and exactly as I want to when I run it locally, in RStudio with the "Run App" feature. The app opens, I hit a button, a new tab opens in my browser asking me to log in, and the app works all the way through. However, once I deploy to shinyapps.io, I can't authenticate. I click on a button to authenticate, and the app just automatically disconnects. It doesn't open a new tab asking me to log in. The shinyapps.io log just tells me to make sure I'm taking steps to run in non-interactive environment which I thought I was doing already.

I'll put my abbreviated code below. I should note that I currently have my OAuth client set to testing mode. Is there something I'm missing about how to specify redirect URLs? I'm not sure since the auth page won't even open. I also have messed around a little bit with googleAuthR but I keep getting a 403 error even on my own computer.

global.R

# import packages
library(shiny)
library(googledrive)
library(googlesheets4)
library(dplyr)
library(stringr)
library(waiter)
library(cli)
library(gargle)
library(bslib)

# set up OAuth client downloaded from Google
drive_auth_configure(path = "path-to-client-json")

###
# global variable set up
###

###
# global function definitions
###

ui.R

ui <- fluidPage(
  useWaiter(),
  waiterShowOnLoad(),
  
  navbarPage(
    "App Title",
    tabPanel("Home",
             mainPanel(
               tags$p("Details about why this exists, who might want to use it, and what it does")
             )
    ),
    
    # Primary page for creating the project ####
    tabPanel("Create the Project!",
             mainPanel(
               
               ## Authenticate ####
               tags$p("Introduction to the app."),
               actionButton("auth", "Authenticate With Google"),
               br(), br(),
               
               ## Appears once authentication has been completed ####
               conditionalPanel(
                 condition = "output.auth_complete==\"yes\"",
                 htmlOutput("greeting")
               )
##### More conditional panels that appear as the user progresses through the questions and app
  ))))
ui

server.R

server <- function(input, output, session) {
  waiter_hide()
  
  rv <- reactiveValues()
  
  # record the name of the user and send a greeting ####
  observeEvent(input$auth, {
 
    drive_auth(email = TRUE, cache = ".secrets/.user")
    gs4_auth(token = drive_token())
    
    rv$auth_complete <- "yes"
    rv$user <- drive_user()$displayName
    
  })
  
  output$greeting <- renderText({
    
    HTML(paste0("Hey ",
                "<span style=\"color: #099392\"><b>", rv$user, "</b></span>",
                "! Welcome to the app........"))
  })
  
  output$auth_complete <- renderText({rv$auth_complete})
  outputOptions(output, "auth_complete", suspendWhenHidden = FALSE)

#### More server items that use functions to operate on Google files
}
server

Local Results

Here is what happens when I run this on my computer.

App project creation page. Sample text with authentication button.

The app loads with the button I created.

Google Sign on page

When I click on the button, a sign on page opens. (Emails hidden for security reasons)

shinyapps.io Results

Disconnected from server error on shinyapps.io

As soon as I hit the same authentication button, the app automatically disconnects :(.

Thanks in advance!!!

2
  • 403 what? Can I see the full error message Commented Apr 23, 2024 at 22:50
  • I did not show the 403 error here since it wasn't my main issue. The 403 error only occurs when I use googleAuthR, specifically gar_shiny_ui(ui). It just says 403 error, you are not allowed to access this page. If I run without gar_shiny_ui() the app works locally but not on shinyapps.io, as described above. Commented Apr 24, 2024 at 20:37

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.