如何配置Google BigQuery命令行工具以使用服务帐户?

How do I configure Google BigQuery command line tool to use a Service Account?

我已经使用Google API控制台创建了一个服务帐户,并希望将此服务帐户与Google BigQuery CLI(bq)工具一起使用。

我一直在使用命令行工具通过?/ .bigquery.v2.token中的有效OAuth2凭据成功访问BigQuery服务,但是我似乎找不到任何有关如何修改此文件(或进行其他配置)的文档工具)来使用服务帐户。

这是我当前的.bigquery.v2.token文件

1
2
3
4
5
6
7
8
9
10
11
12
13
{
   "_module":"oauth2client.client",
   "_class":"OAuth2Credentials",
   "access_token":"--my-access-token--",
   "token_uri":"https://accounts.google.com/o/oauth2/token",
   "invalid": false,
   "client_id":"--my-client-id--.apps.googleusercontent.com",
   "id_token": null,
   "client_secret":"--my-client-secret--",
   "token_expiry":"2012-11-06T15:57:12Z",
   "refresh_token":"--my-refresh-token--",
   "user_agent":"bq/2.0"
}

我的其他文件:?/ .bigqueryrc通常如下所示:

1
2
project_id = --my-project-id--
credential_file = ~/.bigquery.v2.token

我尝试为我的服务帐户将credential_file参数设置为.p12私钥文件,但是没有运气,它给了我以下错误

1
2
3
******************************************************************
** No OAuth2 credentials found, beginning authorization process **
******************************************************************

并要求我转到浏览器中的链接以再次设置我的OAuth2凭据。

命令行工具的初始配置选项" init":

1
bq help init

没有显示有关如何设置此工具以使用服务帐户的有用信息。


我最终找到了一些有关如何进行设置的文档

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
$ bq --help

....

--service_account: Use this service account email address for authorization. For example, [email protected].
(default: '')

--service_account_credential_file: File to be used as a credential store for service accounts. Must be set if using a service account.

--service_account_private_key_file: Filename that contains the service account private key. Required if --service_account is specified.
(default: '')

--service_account_private_key_password: Password for private key. This password must match the password you set on the key when you created it in the Google APIs Console. Defaults to the default Google APIs Console private key password.
(default: 'notasecret')

....

您可以在每个bq(bigquery命令行客户端)请求中专门设置这些设置,即:

1
$ bq --service_account --my-client-id--.apps.googleusercontent.com -- service_account_private_key_file ~/.bigquery.v2.p12 ... [command]

或者您可以在?/ .bigqueryrc文件中设置默认值,如下所示

1
2
3
4
project_id = --my-project-id--
service_account = [email protected]
service_account_credential_file = /home/james/.bigquery.v2.cred
service_account_private_key_file = /home/james/.bigquery.v2.p12

您可以在Google API控制台中找到该服务帐户,并在创建服务帐户时设置了service_account_private_key_password(默认为" notasecret")。

注意:.bigqueryrc中的文件路径必须是完整路径,我无法使用?/ .bigquery ...

需要一些其他依赖项,您将需要通过yum / apt-get安装openssl

1
2
3
4
5
--yum--
$ yum install openssl-devel libssl-devel

--or apt-get--
$ apt-get install libssl-dev

和pyopenssl通过易于安装/ pip

1
2
3
4
5
--easy install--
$ easy_install pyopenssl

--or pip--
$ pip install pyopenssl


现在不建议使用bq授权标志

bq文档


bq工具需要两个配置文件,这些文件由--bigqueryrc和--credential_file标志控制。如果没有找到,bq将在启动过程中尝试自动初始化。

为避免--bigqueryrc文件出现此问题,您可以将" .bigqueryrc"文件放在默认位置,或使用--bigqueryrc将其覆盖到某些可写文件路径。