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

gar_auth() authentication not working #200

Closed abalter closed 3 years ago

abalter commented 3 years ago

What goes wrong

Authentication throwing 400 Authentication Error

Note that this process works correctly with bigrquery.

Steps to reproduce the problem

Please note that if a reproduceable example that I can run is not available, then the likelihood of getting any bug fixed is low.

library(googleAuthR)
gar_auth()

Expected output

Taken to google web authentication page.

Actual output

> library(googleAuthR)
> gar_auth()
i 2021-01-04 21:02:53 > Setting client.id from options(googleAuthR.client_id)
Waiting for authentication in browser...
Press Esc/Ctrl + C to abort

image

Before you run your code, please run:

options(googleAuthR.verbose=2) and copy-paste the console output here.
Check it doesn't include any sensitive info like auth tokens or accountIds - you can usually just edit those out manually and replace with say XXX

> options(googleAuthR.verbose=2)
>

Session Info

> sessionInfo()
R version 3.6.3 (2020-02-29)
Platform: x86_64-w64-mingw32/x64 (64-bit)
Running under: Windows 10 x64 (build 19041)

Matrix products: default

locale:
[1] LC_COLLATE=English_United States.1252
[2] LC_CTYPE=English_United States.1252
[3] LC_MONETARY=English_United States.1252
[4] LC_NUMERIC=C
[5] LC_TIME=English_United States.1252

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

other attached packages:
[1] googleAuthR_1.3.1

loaded via a namespace (and not attached):
 [1] Rcpp_1.0.5       magrittr_2.0.1   R6_2.5.0         rlang_0.4.9
 [5] fansi_0.4.1      httr_1.4.2       tools_3.6.3      cli_2.2.0
 [9] askpass_1.1      ellipsis_0.3.1   openssl_1.4.3    assertthat_0.2.1
[13] digest_0.6.27    tibble_3.0.4     gargle_0.5.0     lifecycle_0.2.0
[17] crayon_1.3.4     later_1.1.0.1    promises_1.1.1   vctrs_0.3.5
[21] fs_1.5.0         curl_4.3         memoise_1.1.0    glue_1.4.2
[25] compiler_3.6.3   pillar_1.4.7     jsonlite_1.7.2   httpuv_1.5.4
[29] pkgconfig_2.0.3

Please run sessionInfo() so we can check what versions of packages you have installed

MarkEdmondson1234 commented 3 years ago

We need more info, such as how you are setting the client.id and what you want scopes you want to authenticate for. The error message suggests the client.id is not set correctly.

abalter commented 3 years ago

Yes--it's hard to provide a reproducible example, especially in that it involves opening a browser.

When I use the bigrquery pacakge, I run bq_auth(email="me@someplace.com"). If I have not already configured my authentication, a google sign in page opens in my browser. I log in, and then I'm all set.

When I use googleAuthR and run gar_auth() a browser window opens and displays the authorization error shown.

Hopefully that explains the issue. Please let me know what other information I can provide to help find the problem.

MarkEdmondson1234 commented 3 years ago

What are you trying to do - use gar_auth() instead of bq_auth()? Perhaps looking at how bigQueryR does it will help: https://github.com/cloudyr/bigQueryR/blob/master/R/auth.R

bqr_auth <- function(json_file = NULL,
                     token = NULL, 
                     email = Sys.getenv("GARGLE_EMAIL")){

  set_scopes()

  if(is.null(json_file)){
    gar_auth(token = token,
             email = email,
             package = "bigQueryR")    
  } else {
    gar_auth_service(json_file = json_file)
  }

}

set_scopes <- function(){
  required_scopes <- c("https://www.googleapis.com/auth/bigquery",
                       "https://www.googleapis.com/auth/cloud-platform")

  op <- getOption("googleAuthR.scopes.selected")
  if(is.null(op)){
    options(googleAuthR.scopes.selected = "https://www.googleapis.com/auth/bigquery")
  } else if(!any(op %in% required_scopes)){
    myMessage("Adding https://www.googleapis.com/auth/bigquery to scopes", level = 3)
    options(googleAuthR.scopes.selected = c(op, "https://www.googleapis.com/auth/bigquery"))
  }
}

They both use gargle underneath.

abalter commented 3 years ago

I'm trying to access GCS (buckets). BigQuery works great.

I'm really trying to use googleCloudStorageR. The only signature for gcs_auth is to use a json file. I thought I'd try the generic authentication in googleAuthR.

MarkEdmondson1234 commented 3 years ago

Ok sounds like you'll be best to use googleCloudStorage's gcs_auth() - v0.6.0 just hit CRAN and it lets you use email as well, or you can use gcs_setup() to create and set the JSON key file for you. I prefer the JSON file since it is more stable and supports long uploads.