I am currently trying to run R scripts from Jenkins via the following plugin (https://plugins.jenkins.io/r/) all on an AWS EC2 instance
The problem now is that I can't authenticate myself in a non-interactive way as indicated by the gargle library documentation.
The gargle cache is well filled (here is the sitrep)
ℹ Taking cache location from the `"gargle_oauth_cache"` option.
1 token found in this gargle OAuth cache:
'~/.cache/gargle'
email app scopes hash...
________________ _______________ ________ __________
[email protected] tidyverse-erato ...drive 928dfaa...
but the drive_auth method always returns the same error:
Error in `drive_auth()`:
! Can't get Google credentials
ℹ Are you running googledrive in a non-interactive session? Consider:
• `drive_deauth()` to prevent the attempt to get credentials
• Call `drive_auth()` directly with all necessary specifics
ℹ See gargle's "Non-interactive auth" vignette for more details:
ℹ <https://gargle.r-lib.org/articles/non-interactive-auth.html>
Backtrace:
▆
1. └─googledrive::drive_auth(...)
2. └─googledrive:::drive_abort(...)
3. └─cli::cli_abort(message = message, ..., .envir = .envir)
4. └─rlang::abort(...)
Execution halted
And so I can't download a remote file placed on my drive:
drive_download(as_id("id"), path = "~/sku.csv", overwrite = TRUE)
sku <- read_csv2("~/sku.csv", locale = locale(encoding = "CP1252"),
col_names = TRUE, col_types = NULL, guess_max = 20000)
print(sku)
my code:
#### R Library ####
library(utils)
library(dplyr)
library(rlang)
library(tidyr)
library(stringr)
library(lubridate)
library(gargle)
library(googledrive)
library(readr)
library(DBI)
library(httr)
library(cronR)
library(RPostgreSQL) # Connect PostgreSQL db
options(encoding = "CP1252")
Sys.setenv(TZ="CET")
default_locale()
## Settings for Google Drive connexion
options(gargle_verbosity = "debug")
options(gargle_oauth_cache = "~/.cache/gargle")
options(httr_oob_default = TRUE)
gargle_oauth_sitrep()
print(gargle_verbosity())
print(sessionInfo())
drive_auth(email = "[email protected]",
scopes = "https://www.googleapis.com/auth/drive",
use_oob = TRUE,
cache = "~/.cache/gargle") # mettre cache = FALSE si on souhaite generer un nouveau token
print(drive_user())
# From Drive
#### From cloud
### Products & co.
## Import
# Sku
drive_download(as_id(id), path = "~/sku.csv", overwrite = TRUE)
sku <- read_csv2("~/sku.csv", locale = locale(encoding = "CP1252"),
#col_names = TRUE, col_types = NULL, guess_max = 20000)
print(sku)
# Permet de reinitialiser les recuperations d'hyperlien sur GOOGLE #
# suite a des updates pour eviter des erreurs #
I also tried to connect to the GUI of my instance in order to connect interactively to fill the cache for the first time and used the same token, but also without success.
I edit this post :
I also precise that I run the same script on Rstudio & Jenkins which are installed on the same instance AWS ec2. I got my token thanks to an authentification page on browser with "drive_auth" on Rstudio.
After "drive_auth()" function written as above, I have :
On Jenkins :
trying `token_fetch()`
trying `credentials_byo_oauth()`
Error caught by `token_fetch()`:
inherits(token, "Token2.0") is not TRUE
trying `credentials_service_account()`
Error caught by `token_fetch()`:
Argument 'txt' must be a JSON string, URL or file.
trying `credentials_external_account()`
adding "userinfo.email" scope
Error caught by `token_fetch()`:
Argument 'txt' must be a JSON string, URL or file.
trying `credentials_app_default()`
trying `credentials_gce()`
trying `credentials_user_oauth2()`
attempt to access internal gargle data from: gargle
Gargle2.0 initialize
adding "userinfo.email" scope
loading token from the cache
no matching token in the cache
initiating new token`
and on Rstudio :
trying `token_fetch()`
trying `credentials_byo_oauth()`
Error caught by `token_fetch()`:
inherits(token, "Token2.0") is not TRUE
trying `credentials_service_account()`
Error caught by `token_fetch()`:
Argument 'txt' must be a JSON string, URL or file.
trying `credentials_external_account()`
adding "userinfo.email" scope
Error caught by `token_fetch()`:
Argument 'txt' must be a JSON string, URL or file.
trying `credentials_app_default()`
trying `credentials_gce()`
trying `credentials_user_oauth2()`
attempt to access internal gargle data from: gargle
Gargle2.0 initialize
adding "userinfo.email" scope
loading token from the cache
matching token found in the cache
Even if Jenkins see the token in the same cache as Rstudio, it doesn't work but it working well on Rstudio. Jenkins wants a new token even if the token is located.
EDIT: I now try to set up the authentification via service account token that i got from the GCP console, i followed these steps (https://gargle.r-lib.org/articles/get-api-credentials.html#service-account-token)
And now I got this error:
trying `token_fetch()`
trying `credentials_byo_oauth()`
Error caught by `token_fetch()`:
inherits(token, "Token2.0") is not TRUE
trying `credentials_service_account()`
adding "userinfo.email" scope
service account email:
'[email protected]'
attempt to access internal gargle data from: googledrive
Error in `map()`:
ℹ In index: 1.
Caused by error in `gargle::response_process()`:
! Client error: (404) Not Found
File not found: 1qUJheGEPlZrzSNlwFBdC1UilwScexlHx.
• message: File not found: 1qUJheGEPlZrzSNlwFBdC1UilwScexlHx.
• domain: global
• reason: notFound
• location: fileId
• locationType: parameter
Backtrace:
▆
1. └─googledrive::drive_download(...)
2. ├─googledrive::as_dribble(file)
3. └─googledrive:::as_dribble.drive_id(file)
4. └─googledrive::drive_get(id = x)
5. ├─googledrive::as_dribble(map(as_id(id), get_one_file_id))
6. └─purrr::map(as_id(id), get_one_file_id)
7. └─purrr:::map_("list", .x, .f, ..., .progress = .progress)
8. ├─purrr:::with_indexed_errors(...)
9. │ └─base::withCallingHandlers(...)
10. ├─purrr:::call_with_cleanup(...)
11. └─googledrive (local) .f(.x[[i]], ...)
12. └─gargle::response_process(response)
13. └─gargle:::gargle_abort_request_failed(error_message(resp), resp)
14. └─gargle:::gargle_abort(...)
15. └─cli::cli_abort(...)
16. └─rlang::abort(...)
Execution halted
Build step 'Execute R script' marked build as failure
Finished: FAILURE