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

mys*_*ous 5 authentication r google-bigquery bigrquery

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

library(bigrquery)
bq_auth()
Run Code Online (Sandbox Code Playgroud)

library(bigrquery)
bq_auth(email="my-service-account-email@myproject.iam.gserviceaccount.com")
Run Code Online (Sandbox Code Playgroud)

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

options("httr_oob_default" = TRUE)
bq_auth(email="my-service-account-email@myproject.iam.gserviceaccount.com")
Run Code Online (Sandbox Code Playgroud)

甚至使用这样的完整列表

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
)
Run Code Online (Sandbox Code Playgroud)

导致错误

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

谁能看到我缺少什么吗?提前致谢。

Bre*_*ton 0

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

\n
    \n
  1. Google Cloud 控制台 (console.cloud.google.com)
  2. \n
  3. 导航菜单
  4. \n
  5. IAM 和管理服务
  6. \n
  7. 账户
  8. \n
  9. 创建服务帐户(创建一个)
  10. \n
  11. 创建Key,并保存到“/path/to/jsonfilename.json”
  12. \n
  13. 在 R Markdown 代码中进行身份验证:bigrquery::bq_auth(path = "/path/to/jsonfilename.json")
  14. \n
\n

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

\n

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

\n