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 51 forks source link

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

Closed OKohesta closed 2 years ago

OKohesta commented 2 years ago

I had some legacy R scripts pulling data from Google Analytics running on the server and accidently deleted the folder, even though cancelled the delete instantly I lost some files and I'm not sure which files. Since then the the scripts pulling from GA are failing.

Steps to reproduce the problem

I downloaded the project .json file from google console and put it on the wd(), I installed re-installed googleAnalyticsR package.

setwd("/home/ubuntu/Google_Analytics") client_id <-"8xxxxxxxxxxxxxxxxxxxxxxxx.apps.googleusercontent.com" client_secret <- "-xxxxxxxxx"

library(jsonlite) library(data.table)

library(googleAnalyticsR,lib="Lib") options(httr_oob_default=TRUE) googleAuthR::gar_set_client('client_secret_xxxxxxxxxxxxxxxxxxxxx.apps.googleusercontent.com.json')

ga_auth(email="the_email_on_gmail_acc")

Expected output

Actual output

Enter authorization code: Error: Could not authenticate via any gargle cred function

Session Info

sessionInfo() R version 3.6.0 (2019-04-26) Platform: x86_64-pc-linux-gnu (64-bit) Running under: Ubuntu 18.04.2 LTS

Matrix products: default BLAS: /usr/lib/x86_64-linux-gnu/openblas/libblas.so.3 LAPACK: /usr/lib/x86_64-linux-gnu/libopenblasp-r0.2.20.so

locale: [1] LC_CTYPE=C.UTF-8 LC_NUMERIC=C LC_TIME=C.UTF-8 [4] LC_COLLATE=C.UTF-8 LC_MONETARY=C.UTF-8 LC_MESSAGES=C.UTF-8 [7] LC_PAPER=C.UTF-8 LC_NAME=C LC_ADDRESS=C [10] LC_TELEPHONE=C LC_MEASUREMENT=C.UTF-8 LC_IDENTIFICATION=C

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

other attached packages: [1] googleAnalyticsR_1.0.1 data.table_1.13.0 jsonlite_1.8.0

loaded via a namespace (and not attached): [1] pillar_1.8.1 compiler_3.6.0 [3] googleAuthR_2.0.0 tools_3.6.0 [5] digest_0.6.29 memoise_2.0.1 [7] lifecycle_1.0.2 gargle_1.2.1 [9] tibble_3.1.8 pkgconfig_2.0.3 [11] rlang_1.0.6 cli_3.4.1 [13] DBI_1.1.0 rstudioapi_0.14 [15] curl_4.3.2 fastmap_1.1.0 [17] withr_2.5.0 dplyr_1.0.10 [19] httr_1.4.4 generics_0.1.3 [21] fs_1.5.2 vctrs_0.4.1 [23] askpass_1.1 rappdirs_0.3.3 [25] tidyselect_1.1.2 glue_1.6.2 [27] R6_2.5.1 fansi_1.0.3 [29] measurementProtocol_0.1.0 purrr_0.3.4 [31] tidyr_1.2.1 magrittr_2.0.3 [33] whisker_0.4 usethis_2.1.6 [35] assertthat_0.2.1 utf8_1.2.2 [37] openssl_2.0.3 cachem_1.0.6

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

MarkEdmondson1234 commented 2 years ago

You set the option for out of band authentication (OOB) but I guess you need to run the script locally again to generate an authentication token to cache.

But I recommend using the json service key approach for authentication on a server - that way you have a file to inspect and it's more stable for situations like this. It involves download a service key and then adding its email as a user to your GA accounts.

OKohesta commented 2 years ago

When I run it with the OOB commented out I get the following error and it just hangs:

ga_auth(email="the_email_on_gmail_acc") Waiting for authentication in browser... Press Esc/Ctrl + C to abort

MarkEdmondson1234 commented 2 years ago

Yes it needs to be run locally to get a token exchange, which is stored in a user folder. I highly recommend switching to json service key instead.

OKohesta commented 2 years ago

I would like to get it working first and then I can looking into getting a service key instead. I ran it locally I found the token in gargle folder in the user folder. I copied it to R server working directory, still get the same error when running script.

Running these commands locally:

googleAuthR::gar_token_info(2)

xxxxxx@gmail.com project_name ...analytics, ...analytics.edit, ...analytics.manage.users, ...analytics.readonly, ...analytics.user.deletion a6b94a2... Scopes: https://www.googleapis.com/auth/analytics https://www.googleapis.com/auth/analytics.edit https://www.googleapis.com/auth/analytics.manage.users https://www.googleapis.com/auth/analytics.readonly https://www.googleapis.com/auth/analytics.user.deletion https://www.googleapis.com/auth/userinfo.email App key: xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxptd.apps.googleusercontent.com

googleAuthR::gar_check_existing_token() FALSE

MarkEdmondson1234 commented 2 years ago

See the bottom of this help file https://gargle.r-lib.org/articles/non-interactive-auth.html

You need to set the google cache location to where the token you generate is uploaded to.

OKohesta commented 2 years ago

Thanks Mark, that has helped resolve the issue. Appreciate all your help.