r - 在 Shiny 的应用程序中使用 bigrquery auth

标签 r google-bigquery shiny

我想创建一个 Shiny 的应用程序,它利用 bigrquery 连接到 BigQuery API 并运行查询。
我使用以下代码来执行查询:

 library(bigrquery)
    project <- "PROJECT_ID" # put your project ID here
    sql <- 'QUERY '
    test <- query_exec(sql, project = project)

但在此之前,bigrquery 包中有一个身份验证过程,例如:
    google <- oauth_endpoint(NULL, "auth", "token",
      base_url = "https://accounts.google.com/o/oauth2")

    bigqr <- oauth_app("google",
      "465736758727.apps.googleusercontent.com",
      "fJbIIyoIag0oA6p114lwsV2r")

    cred <- oauth2.0_token(google, bigqr,
          scope = c(
              "https://www.googleapis.com/auth/bigquery",
              "https://www.googleapis.com/auth/cloud-platform"))

如何在我的应用程序中集成身份验证过程
  • 该过程不需要交互或
  • 该过程适用于给定的应用程序 key 和 secret (我从哪里获得它们?)或
  • 身份验证过程在另一个浏览器窗口中打开。

  • 问候

    最佳答案

    我有一个建议,它类似于我在关于 server-side access to Google Analytics data 的问题中提供的答案。 , 是使用 Google Service Account . googleAuthR 包裹来自 Mark Edmondson可通过 CRAN 获得,提供使用 Google 服务帐户在 R 中执行服务器端身份验证的功能。同一作者的另一个包 bigQueryR ,也在 CRAN 上与 googleAuthR 集成并使用生成的身份验证 token 执行对 Google BigQuery 的查询。

    为达到这个:

  • 为您的 Google API 项目创建一个服务帐号。
  • 下载包含服务帐号私钥的 JSON 文件。
  • 授予服务帐号访问您的 Google BigQuery 项目的权限,就像授予任何其他用户一样。这是通过 Google API 控制台 IAM 屏幕完成的,您可以在其中为项目设置权限。
  • 使用 googleAuthR 进行身份验证时,提供私钥 JSON 文件的位置作为参数。 (见下面的例子。):

  • 以下示例 R 脚本基于 bigrquery 中的示例包,引用包含私钥的 JSON 文件并执行基本的 Google BigQuery 查询。记得设置json_file参数到适当的文件路径和 project您的 Google BigQuery 项目的参数:
    library(googleAuthR)
    library(bigQueryR)
    
    gar_auth_service(
      json_file = "API Project-xxxxxxxxxxxx.json",
      scope = "https://www.googleapis.com/auth/bigquery"
    )
    
    project <- "project_id" # put your project ID here
    sql <- "SELECT year, month, day, weight_pounds
            FROM [publicdata:samples.natality] LIMIT 5"
    
    bqr_query(projectId = project, query = sql, datasetId = "samples")
    

    关于r - 在 Shiny 的应用程序中使用 bigrquery auth,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26263003/

    相关文章:

    使用 ggplot 添加到投影 map 时 R 饼图扭曲

    r - 在 SF 多边形中绘制箭头

    r - 在 R Shiny 应用程序中通过 Shiny 表输入数据

    r - 从 R Shiny 的 selectInput 中过滤

    r - 避免在 sliderTextInput 中重叠文本

    r - 实现平滑的色带

    xml - XMLDocument 内容类对象的 Xpath 错误

    google-bigquery - BigQuery Connected Sheets - 需要用户权限吗?

    sql - 使用 Google BigQuery 的逗号作为带有 IN 子句的 UNION ALL

    java - 如何向我的所有应用引擎实例发送请求