通过R Studio服务器使用BigQuery对谷歌计算引擎上的服务帐户进行身份验证

时间:2022-01-04 15:35:20

I'm looking to call BigQuery from R Studio, installed on a Google Compute Engine.

我希望从R Studio调用BigQuery,它安装在谷歌计算引擎上。

I have the bq python tool installed on the instance, and I was hoping to use its service accounts and system() to get R to call bq command line tool and so get the data.

我在实例上安装了bq python工具,我希望使用它的服务帐户和system()来让R调用bq命令行工具,从而获得数据。

However, I run into authentication problems, where it asks for a browser key. I'm pretty sure there is no need to get the key due to the service account, but I don't know how to construct the authetication from with R (it runs on RStudio, so will have multiple users)

但是,我遇到了身份验证问题,它需要一个浏览器密钥。我很确定,由于服务帐户的原因,不需要获取密钥,但是我不知道如何使用R构造自定义(它在RStudio上运行,所以会有多个用户)

I can get an authetication token like this:

我可以得到这样一个自动标记:

library(RCurl)
library(RJSONIO)
metadata <-  getURL('http://metadata/computeMetadata/v1beta1/instance/service-accounts/default/token')
tokendata <- fromJSON(metadata)

tokendata$$access_token

But how do I then use this to generate a .bigqueryrc token? Its the lack of this that triggers the authetication attempt.

但是如何使用它来生成.bigqueryrc令牌呢?正是由于缺乏这种能力,才引发了自我意识的尝试。

This works ok:

这是好的:

system('/usr/local/bin/bq')

showing me bq is installed ok.

向我展示bq已经安装好了。

But when I try something like:

但是当我尝试的时候

   system('/usr/local/bin/bq ls')

I get this:

我得到了这个:

Welcome to BigQuery! This script will walk you through the process of initializing your .bigqueryrc configuration file.

First, we need to set up your credentials if they do not already exist.


******************************************************************
** No OAuth2 credentials found, beginning authorization process **
******************************************************************

Go to the following link in your browser:

https://accounts.google.com/o/oauth2/auth?scope=https%3A%2F%2Fwww.googleapis.com%2Fauth%2Fbigquery&redirect_uri=urn%3Aietf%3Awg%3Aoauth%3A2.0%3Aoob&response_type=code&client_id=XXXXXXXX.apps.googleusercontent.com&access_type=offline

Enter verification code: You have encountered a bug in the BigQuery CLI. Google engineers monitor and answer questions on Stack Overflow, with the tag google-bigquery:     http://*.com/questions/ask?tags=google-bigquery

etc.

Edit:

编辑:

I have managed to get bq functioning from RStudio system() commands, by skipping the authetication by logging in to the terminal as the user using RStudio, autheticating there by signing in via the browser, then logging back to RStudio and calling system("bq ls") etc..so this is enough to get me going :)

我成功地从RStudio system()命令中获得了bq功能,通过登录到终端作为用户使用RStudio,通过浏览器登录,然后登录到RStudio和调用系统(“bq ls”),从而跳过了自动操作。这就足够让我开始了

However, I would still prefer it if BQ can be autheticated within RStudio itself, as many users may log in and I'll need to autheticate via terminal for all of them. And from the service account documentation, and the fact I can get an authetication token, hints at this being easier.

但是,如果BQ可以在RStudio本身中进行自定义,我还是更喜欢它,因为很多用户可能会登录,我需要通过终端对所有用户进行自定义。从服务帐户文档中,以及我可以获得一个authetication令牌的事实,可以看出这更容易。

1 个解决方案

#1


3  

For the time being, you need to run 'bq init' from the command line to set up your credentials prior to invoking bq from a script in GCE. However, the next release of bq will include support for GCE service accounts via a new --use_gce_service_account flag.

目前,您需要从命令行运行“bq init”,以便在从GCE中的脚本调用bq之前设置凭据。然而,下一个bq版本将通过一个新的——use_gce_service_account标志来支持GCE服务帐户。

#1


3  

For the time being, you need to run 'bq init' from the command line to set up your credentials prior to invoking bq from a script in GCE. However, the next release of bq will include support for GCE service accounts via a new --use_gce_service_account flag.

目前,您需要从命令行运行“bq init”,以便在从GCE中的脚本调用bq之前设置凭据。然而,下一个bq版本将通过一个新的——use_gce_service_account标志来支持GCE服务帐户。