我想创建一个 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"))
如何在我的应用程序中集成身份验证过程
问候
最佳答案
我有一个建议,它类似于我在关于 server-side access to Google Analytics data 的问题中提供的答案。 , 是使用 Google Service Account . googleAuthR
包裹来自 Mark Edmondson可通过 CRAN 获得,提供使用 Google 服务帐户在 R 中执行服务器端身份验证的功能。同一作者的另一个包 bigQueryR
,也在 CRAN 上与 googleAuthR
集成并使用生成的身份验证 token 执行对 Google BigQuery 的查询。
为达到这个:
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/