使用 R Markdown 文档中的 bigrquery 对 Bigquery 进行身份验证

Posted

技术标签:

【中文标题】使用 R Markdown 文档中的 bigrquery 对 Bigquery 进行身份验证【英文标题】:Authentication for Bigquery using bigrquery from an R Markdown document 【发布时间】:2020-05-25 18:28:55 【问题描述】:

我在使用 bigrquery 从我编写的 R Markdown 文档中连接到 GCP 服务帐户时遇到问题。当我从控制台尝试时,身份验证工作正常。两者都有

library(bigrquery)
bq_auth()

library(bigrquery)
bq_auth(email="my-service-account-email@myproject.iam.gserviceaccount.com")

启动带有对话框的浏览器,让我可以按预期使用指定的帐户进行选择和验证。但是在 R Markdown 中,任何尝试都像

options("httr_oob_default" = TRUE)
bq_auth(email="my-service-account-email@myproject.iam.gserviceaccount.com")

甚至像这样使用完整列表

bq_auth(
  email = "my-service-account-email@myproject.iam.gserviceaccount.com",
  path = NULL,
  scopes = c("https://www.googleapis.com/auth/bigquery"),
  cache = gargle::gargle_oauth_cache(),
  use_oob = gargle::gargle_oob_default(),
  token = NULL
)

导致错误

Error: Can't get Google credentials.
Are you running bigrquery in a non-interactive session? Consider:
  * Call `bq_auth()` directly with all necessary specifics.

谁能看到我错过了什么?提前致谢。

【问题讨论】:

【参考方案1】:

您可以下载您的 Google Cloud 服务帐户的 JSON 文件,然后将其用作“bq_auth”函数可以识别的路径。步骤如下:

    谷歌云控制台 (console.cloud.google.com) 导航菜单 IAM 和管理服务 帐户 创建服务帐户(创建一个) 创建Key,并保存到“/path/to/jsonfilename.json” 在您的 R Markdown 代码中进行身份验证:bigrquery::bq_auth(path = "/path/to/jsonfilename.json")

注意:您需要确保将服务帐号设置为有权访问 BigQuery。我将我的设置为“BigQuery Admin”并且它有效,但这可能过于宽泛

从 Elaine See 在媒体上的帖子中借用了这个答案:https://medium.com/@elaine.yl.see/easiest-way-to-use-bigquery-in-r-8af466cd55ca

【讨论】:

另外,请参阅此处的“身份验证和授权”部分:cran.r-project.org/web/packages/bigrquery/readme/README.html 感谢布伦特!可以验证此答案是否有效。我很久以前就争论过,但忘记了问题就在这里。欣赏清晰的表达。 现在互联网将有这些说明以供将来使用!很高兴为您提供帮助

以上是关于使用 R Markdown 文档中的 bigrquery 对 Bigquery 进行身份验证的主要内容,如果未能解决你的问题,请参考以下文章

在 R Markdown 中格式化表格以导出到 MS Word 文档

使用classoption时无法从R markdown生成横向微软word文档:风景

如何在R Markdown中改变biblatex中的引用风格?

使用 R markdown 和 knitr:可以在 YAML 中解释 R 对象

R Markdown 无法获取 RStudio 版本 - knit 中的错误消息

可在 R markdown `asis` 块中反应,不显示循环