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

Error: Could not authenticate via any gargle cred function #192

Closed ogola89 closed 3 years ago

ogola89 commented 4 years ago

What goes wrong

I installed this package and successfully authenticated my own email while having a play with the package using: gar_auth()

However, when I run a new session, it uses the cached email without any option to go ahead with it (which is fine). When instead, I run: gar_auth(email=another_email@domain.com)

I get the following error: Error: Could not authenticate via any gargle cred function

Traceback: Error: Could not authenticate via any gargle cred function 2. stop("Could not authenticate via any gargle cred function", call. = FALSE) 1. gar_auth(email = "another_email@domain.com") Using different emails in the function call all produce the same error (including the one already cached and that works).

Using the cached email, though, doesn't throw any errors at all.

Steps to reproduce the problem

library(googleAuthR)
library(googleAnalyticsR)
library(ggplot2)

gar_auth(email='another_email@domain.com')

This is the entire code up to this point.

Expected output

Expect it to open google and authenticate as it did with the first email.

Actual output

Before you run your code, please run:

options(googleAuthR.verbose=2) and copy-paste the console output here.

Empty output

image

Session Info

R version 4.0.0 (2020-04-24) Platform: x86_64-w64-mingw32/x64 (64-bit) Running under: Windows 10 x64 (build 19041)

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] ggplot2_3.3.2 googleAnalyticsR_0.8.0 googleAuthR_1.3.0

loaded via a namespace (and not attached): [1] rstudioapi_0.11 knitr_1.29 magrittr_1.5 munsell_0.5.0 tidyselect_1.1.0 colorspace_1.4-1 R6_2.4.1
[8] rlang_0.4.7 fansi_0.4.1 httr_1.4.2 dplyr_1.0.1 tools_4.0.0 grid_4.0.0 gtable_0.3.0
[15] xfun_0.15 cli_2.0.2 withr_2.2.0 askpass_1.1 ellipsis_0.3.1 openssl_1.4.2 assertthat_0.2.1 [22] digest_0.6.25 tibble_3.0.3 gargle_0.5.0 lifecycle_0.2.0 crayon_1.3.4 tidyr_1.1.1 purrr_0.3.4
[29] vctrs_0.3.2 fs_1.5.0 curl_4.3 memoise_1.1.0 glue_1.4.1 compiler_4.0.0 pillar_1.4.6
[36] scales_1.1.1 generics_0.0.2 jsonlite_1.7.0 pkgconfig_2.0.3

MarkEdmondson1234 commented 4 years ago

I think this is because the expectation is you only add the "email" parameter after you have generated a token for it e.g. you authenticate without email filled in, then choose 0 in the menu to create a new token.

When you use the email argument, its saying "look for a token with this email hash" - if it doesn't exist already it doesn't create a new token (I think) - this could maybe be supported but probably a reason why not, that you could ask on gargles end. If it is like that already and its googleAuthR thats putting in the complication, let me know and I'll support it.

MarkEdmondson1234 commented 4 years ago

In any case, for the example you give here you should not need to use googleAuthR at all - only load googleAnalyticsRand use ga_auth():

library(googleAnalyticsR)
library(ggplot2)

ga_auth(email='another_email@domain.com')
ogola89 commented 4 years ago

Hi Mark,

Thanks for the quick response. I will try on gargle as I still got the error without loading googleAuthR - not really sure if it's still loading googleAuthR in the background - it's included in the via namespace and not attached packages:

> ga_auth(email='new_email@domain.co.uk')
i 2020-08-11 17:54:21 > Default Google Project for googleAnalyticsR is set.  
 This is shared with all googleAnalyticsR users. 
 If making a lot of API calls, please: 
 visit: https://bit.ly/2Evk6hn 
 for instructions on setting your own Google Project 

i 2020-08-11 17:54:21 > Setting client.id from options(googleAuthR.client_id)
Error: Could not authenticate via any gargle cred function
> sessionInfo()
R version 4.0.0 (2020-04-24)
Platform: x86_64-w64-mingw32/x64 (64-bit)
Running under: Windows 10 x64 (build 19041)

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] ggplot2_3.3.2          googleAnalyticsR_0.8.0

loaded via a namespace (and not attached):
 [1] rstudioapi_0.11   knitr_1.29        magrittr_1.5      munsell_0.5.0     tidyselect_1.1.0  colorspace_1.4-1  R6_2.4.1         
 [8] rlang_0.4.7       fansi_0.4.1       httr_1.4.2        dplyr_1.0.1       tools_4.0.0       grid_4.0.0        gtable_0.3.0     
[15] xfun_0.15         cli_2.0.2         withr_2.2.0       googleAuthR_1.3.0 askpass_1.1       ellipsis_0.3.1    openssl_1.4.2    
[22] assertthat_0.2.1  digest_0.6.25     tibble_3.0.3      gargle_0.5.0      lifecycle_0.2.0   crayon_1.3.4      tidyr_1.1.1      
[29] purrr_0.3.4       vctrs_0.3.2       fs_1.5.0          curl_4.3          memoise_1.1.0     glue_1.4.1        compiler_4.0.0   
[36] pillar_1.4.6      scales_1.1.1      generics_0.0.2    jsonlite_1.7.0    pkgconfig_2.0.3  
`
MarkEdmondson1234 commented 4 years ago

Thanks for raising the issue on https://github.com/r-lib/gargle/issues/151 - this confirms it is something that should be fixed here

ogola89 commented 4 years ago

Thanks for raising the issue on r-lib/gargle#151 - this confirms it is something that should be fixed here

OK great, thanks, will await to hear from you on this

MarkEdmondson1234 commented 4 years ago

This looks to be linked to the default client.id as it works as expected when a client.id is specified:

> library(googleAnalyticsR)
ℹ 2020-08-12 06:57:10 > Setting client.id from  my-client-id.json
> library(googleAuthR)
> gar_auth(email="mark@blah.com")
> ga_account_list()
# A tibble: 3,841 x 11
   accountId accountName internalWebProp… level websiteUrl type  starred
   <chr>     <chr>       <chr>            <chr> <chr>      <chr> <lgl>  
....etc... 

But it still gets in the way when using only ga_auth()

> ga_auth(email="mark@blah.com")
Error in if (token$auth_token$secrets$type == "service_account") return(FALSE) : 
  argument is of length zero
ogola89 commented 4 years ago

Hi Mark, any update on this issue?

MarkEdmondson1234 commented 4 years ago

Not yet, but it shouldn't be blocking anything right? You can auth without the email argument first in the console, then use it in subsequent authentication. The bug is only if its specified the first time.

ogola89 commented 3 years ago

Hi Mark,

Apologies for the delayed reply, I paused this project for a bit of time, but I have just tried it and getting the same response:

> library(googleAnalyticsR)
Warning messages:
1: In readLines(file, skipNul = TRUE) :
  cannot open compressed file 'C:/Program Files/R/R-4.0.2/library/lsei/DESCRIPTION', probable reason 'No such file or directory'
2: In readLines(file, skipNul = TRUE) :
  cannot open compressed file 'C:/Program Files/R/R-4.0.2/library/npsurv/DESCRIPTION', probable reason 'No such file or directory'
> ga_auth(email="anotheremail@gmail.com")
i 2020-10-14 11:37:48 > Default Google Project for googleAnalyticsR is set.  
 This is shared with all googleAnalyticsR users. 
 If making a lot of API calls, please: 
 visit: https://bit.ly/2Evk6hn 
 for instructions on setting your own Google Project 

i 2020-10-14 11:37:48 > Setting client.id from options(googleAuthR.client_id)
Error: Could not authenticate via any gargle cred function

Traceback:

Error: Could not authenticate via any gargle cred function
3.
stop("Could not authenticate via any gargle cred function", call. = FALSE)
2.
gar_auth(token = token, email = email, package = "googleAnalyticsR")
1.
ga_auth(email = "anotheremail@gmail.com")
MarkEdmondson1234 commented 3 years ago

Can you try to auth without the email first eg.

library(googleAnalyticsR)
ga_auth()

Authenticate in the browser with your email "anotheremail@gmail.com".

Then on subsequent R sessions you can specify it to avoid selecting from the menu:

library(googleAnalyticsR)
ga_auth(email="anotheremail@gmail.com")
ogola89 commented 3 years ago

The ga_auth() method works but uses the cached email. I want to use this for a few different emails and that's where the problem lies. Is there a way I can clear the cache?

I have tried running options(gargle_oauth_email="another mail @gmail.com") before running the ga_auth but it doesn't seem to be doing anything.

I added the traceback to the previous post.

MarkEdmondson1234 commented 3 years ago

Do you not get a menu to select the first ga_auth() - I guess your options or something is not starting from a clean state then, as you should see

The googleAnalyticsR package is requesting access to your Google account. Select a pre-authorised account or enter '0' to obtain a new token. Press Esc/Ctrl + C to abort.

1: my@email.com
ogola89 commented 3 years ago

I haven't seen that message/menu before.

This is what I see when running ga_auth() with no options passed inside:


> ga_auth()
i 2020-10-14 13:15:12 > Default Google Project for googleAnalyticsR is set.  
 This is shared with all googleAnalyticsR users. 
 If making a lot of API calls, please: 
 visit: https://bit.ly/2Evk6hn 
 for instructions on setting your own Google Project 

Suitable tokens found in the cache, associated with these emails:
  * firstemail@gmail.com
  * secondemail@domain.co.uk
The first will be used.
Using an auto-discovered, cached token.
To suppress this message, modify your code or options to clearly consent to the use of a cached token.
See gargle's "Non-interactive auth" vignette for more details:
https://gargle.r-lib.org/articles/non-interactive-auth.html
The googleAnalyticsR package is using a cached token for firstemail@gmail.com.
ogola89 commented 3 years ago

Hi Mark, I have found a workaround actually.

googleAuthR::gar_auth(email='anyotheremail@gmail.com')

The above code seems to have activated the webpage authentication.

I am now able to use the new email's authentication for GA queries.