在闪亮的应用程序中使用 bigrquery auth [英] Use bigrquery auth in shiny application

查看:18
本文介绍了在闪亮的应用程序中使用 bigrquery auth的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I want to create a shiny application which makes use of the bigrquery to connect to the BigQuery API and run a query. I use the following code to execute the query:

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

但在此之前,bigrquery 包中有一个身份验证过程,例如:

But before this there is an authentication process in the bigrquery package like:

    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"))

如何在我的应用程序中集成身份验证过程

How can I integrate the auth process in my application that

  • 该过程不需要交互或
  • 该过程使用给定的应用密钥和机密(我从哪里获得它们?)或
  • 身份验证过程在另一个浏览器窗口中打开.

问候

推荐答案

我有一个建议,类似于我在关于 服务器端访问 Google Analytics 数据,是使用 Google服务帐号.googleAuthR 包由 Mark Edmondson 提供,可通过 CRAN 获得,提供使用 Google 执行 R 中的服务器端身份验证的功能服务帐户.同一作者的另一个名为 bigQueryR 的包也在 CRAN 上集成了googleAuthR 并使用生成的身份验证令牌执行对 Google BigQuery 的查询.

One suggestion I have, which is similar to an answer I provided on a question about server-side access to Google Analytics data, is to use a Google Service Account. The googleAuthR package by Mark Edmondson, available through CRAN, provides functionality to perform server-side authentication in R using a Google Service Account. Another package by the same author called bigQueryR, also on CRAN, integrates with googleAuthR and uses the resulting authentication token to execute queries to Google BigQuery.

要实现这一点:

  1. 为您的 Google API 项目创建一个服务帐户.
  2. 下载包含服务帐号私钥的 JSON 文件.
  3. 授予服务帐户访问您的 Google BigQuery 项目的权限,就像授予任何其他用户一样.这是通过 Google API 控制台 IAM 屏幕完成的,您可以在其中为项目设置权限.
  4. 在使用 googleAuthR 进行身份验证时,将私钥 JSON 文件的位置作为参数提供(请参见下面的示例.):
  1. Create a service account for your Google API project.
  2. Download the JSON file containing the private key of the service account.
  3. Grant the service account access to your Google BigQuery project, in the same way as you would for any other user. This is done via the Google API console IAM screen where you set permissions for your project.
  4. Supply the location of the private key JSON file as an argument when authenticating with googleAuthR (see the example below.):

以下示例 R 脚本,基于 bigrquery 包,引用包含私钥的 JSON 文件并执行基本的 Google BigQuery 查询.请记住将 json_file 参数设置为适当的文件路径,并将 project 参数设置为您的 Google BigQuery 项目:

The following example R script, based off an example from the bigrquery package, references the JSON file containing the private key and performs a basic Google BigQuery query. Remember to set the json_file argument to the appropriate file path and the project argument to your Google BigQuery project:

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")

这篇关于在闪亮的应用程序中使用 bigrquery auth的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆