在R Colab中挂载谷歌云盘遭遇drive_auth认证错误的技术咨询
R Colab 连接谷歌云盘的认证错误解决
问题场景
在R Colab环境中尝试连接谷歌云盘,执行以下代码后触发认证错误:
if (file.exists("/usr/local/lib/python3.6/dist-packages/google/colab/_ipython.py")) { install.packages("R.utils") library("R.utils") library("httr") my_check <- function() {return(TRUE)} reassignInPackage("is_interactive", pkgName = "httr", my_check) options(rlang_interactive=TRUE ) }
认证代码:
drive_auth( email = gargle::gargle_oauth_email(), path = NULL, scopes = "https://www.googleapis.com/auth/drive", cache = gargle::gargle_oauth_cache(), use_oob = gargle::gargle_oob_default(), token = NULL )
错误信息
执行drive_auth()时出现以下错误:
! 无法获取Google凭据
ℹ 您是否在非交互式会话中运行googledrive?建议:
• 使用drive_deauth()避免尝试获取凭据
• 直接调用drive_auth()并传入所有必要参数
回溯信息:
1. drive_auth(use_oob = TRUE, cache = TRUE) 2. drive_abort(c("Can't get Google credentials", i = "Are you running googledrive in a non-interactive session? \\n Consider:", . `*` = "{.fun drive_deauth} to prevent the attempt to get credentials", . `*` = "Call {.fun drive_auth} directly with all necessary specifics", . i = "See gargle's \"Non-interactive auth\" vignette for more details:", . i = "{.url https://gargle.r-lib.org/articles/non-interactive-auth.html}")) 3. cli::cli_abort(message = message, ..., .envir = .envir) 4. rlang::abort(message, ..., call = call, use_cli_format = TRUE, . .frame = .frame) 5. signal_abort(cnd, .file)
解决方法
- 强制启用OOB认证模式:Colab的R环境无法被默认识别为交互式环境,需手动指定
use_oob = TRUE并开启缓存,修改后的认证代码如下:
drive_auth( use_oob = TRUE, cache = TRUE, scopes = "https://www.googleapis.com/auth/drive" )
执行后会生成认证链接,复制链接在浏览器打开,登录谷歌账号获取授权码,粘贴回Colab输入框即可完成认证。
- 确保交互式配置生效:执行完环境配置代码后,重新加载
googledrive包,确保is_interactive函数的修改生效,添加代码:
library(googledrive)
内容的提问来源于stack exchange,提问作者Danielle Carmem Porto Lucena
相关产品推荐
相关产品推荐

