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

Google bigquery 如何配置GoogleBigQuery命令行工具以使用服务帐户?,google-bigquery,Google Bigquery,我已经使用GoogleAPI控制台创建了一个服务帐户,并希望通过(bq)工具使用这个服务帐户 我一直在使用命令行工具在~/.BigQuery.v2.token中使用有效的OAuth2凭据成功访问BigQuery服务,但是我似乎找不到任何关于如何修改此文件(或以其他方式配置工具)以使用服务帐户的文档 这是我当前的.bigquery.v2.token文件 { "_module": "oauth2client.client", "_class": "OAuth2Credentials"

我已经使用GoogleAPI控制台创建了一个服务帐户,并希望通过(bq)工具使用这个服务帐户

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

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

{
    "_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通常如下所示:

project_id = --my-project-id--
credential_file = ~/.bigquery.v2.token
我已尝试将credential_file参数设置为我的服务帐户的.p12私钥文件,但没有成功,它返回了以下错误

******************************************************************
** No OAuth2 credentials found, beginning authorization process **
******************************************************************
并要求我转到浏览器中的链接以再次设置OAuth2凭据

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


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

我找到了一些关于如何设置此工具的文档

$ bq --help

....

--service_account: Use this service account email address for authorization. For example, 1234567890@developer.gserviceaccount.com.
(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命令行客户端)请求上专门设置它们,即:

$ bq --service_account --my-client-id--.apps.googleusercontent.com -- service_account_private_key_file ~/.bigquery.v2.p12 ... [command]
或者您可以在~/.bigqueryrc文件中设置默认值,如下所示

project_id = --my-project-id--
service_account = --my-client-id--@developer.gserviceaccount.com
service_account_credential_file = /home/james/.bigquery.v2.cred
service_account_private_key_file = /home/james/.bigquery.v2.p12
服务帐户可以在谷歌API控制台中找到,您可以在创建服务帐户时设置服务帐户密码(默认为“notasecret”)

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

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

--yum--
$ yum install openssl-devel libssl-devel

--or apt-get--
$ apt-get install libssl-dev
通过简易安装/pip和pyopenssl

--easy install--
$ easy_install pyopenssl

--or pip--
$ pip install pyopenssl

bq工具需要两个配置文件,由--bigqueryrc和--credential_file标志控制。如果两者都未找到,bq将尝试在启动期间自动初始化


为了避免--bigqueryrc文件出现这种情况,您可以在默认位置放置一个“.bigqueryrc”文件,或者用--bigqueryrc将其覆盖到某个可写文件路径。

现在不推荐使用bq授权标志


至少在OS X上,遵循使用docker映像创建服务帐户的说明更容易:在bq cli上使用服务帐户凭据的正确方法是使用“gcloud auth activate service account”命令激活它,然后在没有任何验证标志的情况下运行bq。这些bq标志仅供gcloud内部使用。请不要设置它们,它会干扰gcloud并导致无法预测的结果。
--easy install--
$ easy_install pyopenssl

--or pip--
$ pip install pyopenssl