You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

AWS EC2上Jenkins运行R脚本无法下载Google Drive文件

AWS EC2实例上Jenkins运行R脚本访问Google Drive的问题

问题1:非交互式认证失败

按照gargle库文档配置非交互式认证,gargle缓存已存在有效token,缓存状态如下:

ℹ Taking cache location from the `"gargle_oauth_cache"` option.
1 token found in this gargle OAuth cache:
'~/.cache/gargle'

email            app             scopes   hash...   
________________ _______________ ________ __________
secret@secret.fr tidyverse-erato ...drive 928dfaa...

但调用drive_auth()时始终返回如下错误:

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

尝试通过实例GUI交互式生成token后复用,仍未解决。同实例上的RStudio可正常识别缓存token完成认证,但Jenkins无法匹配缓存中的token,始终尝试生成新token:

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`

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

问题2:服务账号认证后文件访问失败

改用GCP控制台获取的服务账号token认证,出现404错误,提示文件不存在:

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:
'googledrive-sheets@exemple.iam.gserviceaccount.com'
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

我的R代码

#### 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 = "secret@secret.com",
          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 #

内容的提问来源于stack exchange,提问作者Melly

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.31 16:00:59