I'm trying to create an shiny app that first do the authorization with OAuth (see https://developers.google.com/identity/sign-in/web/sign-in) and then takes a token and use is to access a google sheet using googlesheets4
library. Sign-in process works correct, meaning I can see my email (and other data from googleUser.getBasicProfile()
. It seems though that no id_token
is returned. Minimal example:
app.R
library(shiny)
library(shinyjs)
library(googlesheets4)
shinyApp(
ui = fluidPage(
useShinyjs(),
tags$head(tags$script(src="https://apis.google.com/js/platform.js")),
tags$meta(name = "google-signin-scope", content = "profile email"),
tags$meta(name = "google-signin-client_id", content = "<CLIENT_ID>.apps.googleusercontent.com"),
includeScript("signin.js"),
div(id = "signin", class = "g-signin2", "data-onsuccess" = "onSignIn"),
actionButton("signout", "Sign Out", onclick="signOut();", class="btn-danger"),
with(tags, dl(dt("Email"), dd(textOutput("g.email")),
dt("Token"), dd(textOutput("g.id_token"))
)),
tableOutput("df")
),
server = function(input, output) {
output$g.email = renderText({ input$g.email }) # here my email is printed in the app
output$g.id_token = renderText({ input$g.id_token}) # no id_token available?
output$df <- renderTable({
sheets_auth(token = input$g.id_token)
read_sheet("<GOOGLE_SHEET_ID>")
})
}
)
and singin.js
function onSignIn(googleUser) {
var profile = googleUser.getBasicProfile();
Shiny.onInputChange("g.email", profile.getEmail());
var id_token = googleUser.getAuthResponse().id_token;
Shiny.onInputChange("g.id_token", id_token);
}
function signOut() {
var auth2 = gapi.auth2.getAuthInstance();
auth2.signOut();
Shiny.onInputChange("g.email", null);
Shiny.onInputChange("g.id_token", null);
}
How can I access id_token
and pass it to sheets_auth
function?