MarkEdmondson1234 / googleAuthR

Google API Client Library for R. Easy authentication and help to build Google API R libraries with OAuth2. Shiny compatible.
https://code.markedmondson.me/googleAuthR
Other
175 stars 54 forks source link

Cannot figure out how to get user name / login e-mail #194

Closed JimboMahoney closed 3 years ago

JimboMahoney commented 3 years ago

What goes wrong

Error in user_details() : could not find function "user_details".

(I'm still quite new to R and Shiny, but I've tried Googling all over the place for solutions, but cannot figure anything out!

Steps to reproduce the problem

if (!require("shiny")) {
  devtools::install_github("shiny")
  library(shiny)
}

if (!require("googleAuthR")) {
  install.packages("googleAuthR", dependencies = TRUE)
  library(googleAuthR)
}

if (!require("googleID")) {
  devtools::install_github("MarkEdmondson1234/googleID")
  library(googleID)
}

options(googleAuthR.scopes.selected = c("https://www.googleapis.com/auth/userinfo.email",
                                        "https://www.googleapis.com/auth/userinfo.profile"))
options("googleAuthR.webapp.client_id" = "MYID")
options("googleAuthR.webapp.client_secret" = "MYSECRET")

server <- function(input, output, session){

  ## Create access token and render login button
  access_token <- callModule(googleAuth, "loginButton")

  ## to use in a shiny app:
  user_details <- reactive({

    req(access_token())

    with_shiny(get_user_info, shiny_access_token = access_token())
  })
}

## ui.R
ui <- fluidPage(

  googleAuthUI("loginButton"),

  req(user_details()),

  h1("Hello ", user_details()$displayName)

)

shinyApp(ui = ui, server = server)

Expected output

Display login button and user who is logged in after authentication.

Actual output

Code doesn't run unless I remove the following lines from UI:

  req(user_details()),

  h1("Hello ", user_details()$displayName)

When I remove them, the code runs and shows me a login button, which seems to work as I get redirected to Google to sign in.

Session Info

R version 3.6.2 (2019-12-12) Platform: x86_64-w64-mingw32/x64 (64-bit) Running under: Windows 10 x64 (build 17134)

Matrix products: default

locale: [1] LC_COLLATE=English_United Kingdom.1252 LC_CTYPE=English_United Kingdom.1252 LC_MONETARY=English_United Kingdom.1252 [4] LC_NUMERIC=C LC_TIME=English_United Kingdom.1252

attached base packages: [1] stats graphics grDevices utils datasets methods base

other attached packages: [1] googleAuthR_1.3.0 shiny_1.5.0 googleID_0.0.9001

loaded via a namespace (and not attached): [1] Rcpp_1.0.5 compiler_3.6.2 later_1.1.0.1 prettyunits_1.1.1 remotes_2.2.0 tools_3.6.2 testthat_2.3.2
[8] digest_0.6.25 pkgbuild_1.1.0 pkgload_1.1.0 jsonlite_1.7.1 gargle_0.5.0 memoise_1.1.0 rlang_0.4.7
[15] cli_2.0.2 rstudioapi_0.11 curl_4.3 fastmap_1.0.1 httr_1.4.2 withr_2.2.0 desc_1.2.0
[22] fs_1.5.0 devtools_2.3.1 rprojroot_1.3-2 glue_1.4.2 R6_2.4.1 processx_3.4.4 fansi_0.4.1
[29] sessioninfo_1.1.1 callr_3.4.4 magrittr_1.5 backports_1.1.10 ps_1.3.4 promises_1.1.1 ellipsis_0.3.0
[36] htmltools_0.5.0 usethis_1.6.1 rsconnect_0.8.16 assertthat_0.2.1 mime_0.9 xtable_1.8-4 httpuv_1.5.4
[43] crayon_1.3.4

MarkEdmondson1234 commented 3 years ago

Sorry GoogleID won’t work anymore as it used the deprecated Google+ API. But the JavaScript modules such as Sign In may help you.


From: James notifications@github.com Sent: Wednesday, September 16, 2020 2:26:44 PM To: MarkEdmondson1234/googleAuthR googleAuthR@noreply.github.com Cc: Subscribed subscribed@noreply.github.com Subject: [MarkEdmondson1234/googleAuthR] Cannot figure out how to get user name / login e-mail (#194)

What goes wrong

Error in user_details() : could not find function "user_details".

(I'm still quite new to R and Shiny, but I've tried Googling all over the place for solutions, but cannot figure anything out!

Steps to reproduce the problem

if (!require("shiny")) { devtools::install_github("shiny") library(shiny) }

if (!require("googleAuthR")) { install.packages("googleAuthR", dependencies = TRUE) library(googleAuthR) }

if (!require("googleID")) { devtools::install_github("MarkEdmondson1234/googleID") library(googleID) }

options(googleAuthR.scopes.selected = c("https://www.googleapis.com/auth/userinfo.email", "https://www.googleapis.com/auth/userinfo.profile")) options("googleAuthR.webapp.client_id" = "MYID") options("googleAuthR.webapp.client_secret" = "MYSECRET")

server <- function(input, output, session){

Create access token and render login button

access_token <- callModule(googleAuth, "loginButton")

to use in a shiny app:

user_details <- reactive({

req(access_token())

with_shiny(get_user_info, shiny_access_token = access_token())

}) }

ui.R

ui <- fluidPage(

googleAuthUI("loginButton"),

req(user_details()),

h1("Hello ", user_details()$displayName)

)

shinyApp(ui = ui, server = server)

Expected output

Display login button and user who is logged in after authentication.

Actual output

Code doesn't run unless I remove the following lines from UI:

req(user_details()),

h1("Hello ", user_details()$displayName)

When I remove them, the code runs and shows me a login button, which seems to work as I get redirected to Google to sign in.

Session Info

R version 3.6.2 (2019-12-12) Platform: x86_64-w64-mingw32/x64 (64-bit) Running under: Windows 10 x64 (build 17134)

Matrix products: default

locale: [1] LC_COLLATE=English_United Kingdom.1252 LC_CTYPE=English_United Kingdom.1252 LC_MONETARY=English_United Kingdom.1252 [4] LC_NUMERIC=C LC_TIME=English_United Kingdom.1252

attached base packages: [1] stats graphics grDevices utils datasets methods base

other attached packages: [1] googleAuthR_1.3.0 shiny_1.5.0 googleID_0.0.9001

loaded via a namespace (and not attached): [1] Rcpp_1.0.5 compiler_3.6.2 later_1.1.0.1 prettyunits_1.1.1 remotes_2.2.0 tools_3.6.2 testthat_2.3.2 [8] digest_0.6.25 pkgbuild_1.1.0 pkgload_1.1.0 jsonlite_1.7.1 gargle_0.5.0 memoise_1.1.0 rlang_0.4.7 [15] cli_2.0.2 rstudioapi_0.11 curl_4.3 fastmap_1.0.1 httr_1.4.2 withr_2.2.0 desc_1.2.0 [22] fs_1.5.0 devtools_2.3.1 rprojroot_1.3-2 glue_1.4.2 R6_2.4.1 processx_3.4.4 fansi_0.4.1 [29] sessioninfo_1.1.1 callr_3.4.4 magrittr_1.5 backports_1.1.10 ps_1.3.4 promises_1.1.1 ellipsis_0.3.0 [36] htmltools_0.5.0 usethis_1.6.1 rsconnect_0.8.16 assertthat_0.2.1 mime_0.9 xtable_1.8-4 httpuv_1.5.4 [43] crayon_1.3.4

— You are receiving this because you are subscribed to this thread. Reply to this email directly, view it on GitHubhttps://github.com/MarkEdmondson1234/googleAuthR/issues/194, or unsubscribehttps://github.com/notifications/unsubscribe-auth/AAYCPLCCAT7JW6HHOS3IT6LSGCVIJANCNFSM4ROYCUDQ.

JimboMahoney commented 3 years ago

Thanks for the fast response - can you offer any more clues or examples?

I'm trying to create a Shiny app that reads and writes data to a Google Sheet, so I'm using googlesheets4.

However, I'm really struggling with the authentication side of things - it's easy when the sheet is open to all and I can just

gs4_deauth()

but trying to authenticate and then do something simple like read which user is logged in is proving to be very difficult because I cannot find any recent, working examples!

e.g. this looked great, but relies on googleID:

https://lmyint.github.io/post/shiny-app-with-google-login/

MarkEdmondson1234 commented 3 years ago

Ah well googlesheets4 is another issue, its not shiny compatible despite we're both using gargle underneath - the permissions you need for accessing it are very hard to get (you have to submit a video showing your security measures for your own GCP project etc). See https://github.com/tidyverse/googlesheets4/issues/66 for history. Its tracked at https://github.com/r-lib/gargle/issues/14 - since its talked about a lot there unless you need more general shiny auth help then I'll close this issue?

MarkEdmondson1234 commented 3 years ago

If you don't need authentication, you can get some way with a deauthed Googlesheet but asking for user's own GoogleDrive access is hard.

JimboMahoney commented 3 years ago

Ouch.

Thanks for the info - yes, I'll close this issue and figure out some way of doing this.

Reading from Google Sheets using googlesheets4 and Shiny is easy, but writing seems very hard (in a non-interactive session).

MarkEdmondson1234 commented 3 years ago

If its just your authentication details, then using a service account key to authenticate is the best way, you can read/write etc fine, just include the service key with your Shiny app. The problems start if you want end users of the Shiny app to register auth with their own sheets, that you do not normally have access too.

MarkEdmondson1234 commented 3 years ago

One alternative could be to ask users to upload their own service JSON key (created from their own GCP project), but thats obviously more involved than clicking a signin button.

JimboMahoney commented 3 years ago

Thanks - I went with the upload of my service key.

It's not quite as nice as having users authenticate, but this will suffice for my needs.