0

I am working in an app that save tokens (access and refresh tokens) from users in order to daily extract fitness data (Google Fitness). I achieved to make it work locally. This is my MWE:

library(shiny)
library(httr)

if (Sys.getenv('SHINY_PORT') == ""){
  #LOCAL
  print("Local")
  myPort=8080
  options(shiny.port = myPort)
  APP_URL<-"http://localhost:8080/"
  
} else {
  #SHINYAPPS.IO
  print("shinyapps.io")
  APP_URL <- "https://l72mtf-pavlo-ccall.shinyapps.io/fitness/"
}

# web
google_client <- gargle::gargle_oauth_client_from_json("client_secret_138305270894-phvsangonh44nkbtnr7mihd658icfagc.apps.googleusercontent.com.json")

app <- httr::oauth_app(appname = "myapp", key = google_client$id, secret = google_client$secret,redirect_uri = APP_URL)
api <- oauth_endpoints("google")
scope <- "https://www.googleapis.com/auth/fitness.activity.read https://www.googleapis.com/auth/userinfo.profile https://www.googleapis.com/auth/userinfo.email"

url_has_param <- function(params,code) {return(!is.null(params[[code]]))}


uiFunc <- function(req) {
  
  params<-parseQueryString(req$QUERY_STRING)
  
  if (!url_has_param(params,"code")) {

      url <- oauth2.0_authorize_url(api, app, scope = scope)
      #Make it offline
      url <- paste0(url,"&access_type=offline")
      
      redirect <- sprintf("location.replace(\"%s\");", url)
      tags$script(HTML(redirect))
    
  } else {
    
    body=list(
      client_id=google_client$id,
      client_secret=google_client$secret,
      code = params$code,
      grant_type="authorization_code",
      redirect_uri=APP_URL
    )
    
    response<-httr::POST("https://oauth2.googleapis.com/token",body=body,encode="form")
    responseToken<-content(response,"parsed")
    print("responseToken")
    print(responseToken)

  }
}

server <- function(input, output, session) {
  
  params <- parseQueryString(isolate(session$clientData$url_search))
  if (!url_has_param(params,"code")){return()}
  
}

# Note that we're using uiFunc, not ui!
shinyApp(uiFunc, server)

When I execute it in local, I get the refresh token:

enter image description here

However, when I deploy it to shinyapps.io. The refresh token does not appear:

enter image description here

Any idea what I am doing wrong?

Lev
  • 693
  • 1
  • 8
  • 24
  • 1
    See this [question](https://stackoverflow.com/q/53342863/8016720). The solution is at the bottom of my question. – John Hanley Jun 18 '23 at 17:13

0 Answers0