我使用Google API控制台创建了一个服务帐户,并希望将此服务帐户与Google BigQuery CLI(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"
}
Run Code Online (Sandbox Code Playgroud)
我的另一个文件:〜/ .bigqueryrc通常看起来像这样:
project_id = --my-project-id--
credential_file = ~/.bigquery.v2.token
Run Code Online (Sandbox Code Playgroud)
我已经尝试将credential_file参数设置为我的服务帐户的.p12私钥文件,但没有运气,它给了我以下错误
******************************************************************
** No OAuth2 credentials found, beginning authorization process **
******************************************************************
Run Code Online (Sandbox Code Playgroud)
并要求我转到浏览器中的链接以再次设置我的OAuth2凭据.
命令行工具的初始配置选项"init":
bq help init
Run Code Online (Sandbox Code Playgroud)
没有显示有关如何设置此工具以使用服务帐户的有用信息.
我最终找到了一些关于如何设置它的文档
$ 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')
....
Run Code Online (Sandbox Code Playgroud)
您可以在每个bq(bigquery命令行客户端)请求上专门设置这些请求,即:
$ bq --service_account --my-client-id--.apps.googleusercontent.com -- service_account_private_key_file ~/.bigquery.v2.p12 ... [command]
Run Code Online (Sandbox Code Playgroud)
或者您可以在〜/ .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
Run Code Online (Sandbox Code Playgroud)
您可以在Google API控制台中找到该服务帐户,并在创建服务帐户时设置service_account_private_key_password(默认为"notasecret").
注意: .bigqueryrc中的文件路径必须是完整路径,我无法使用〜/ .bigquery ...
需要一些额外的依赖项,您需要通过yum/apt-get安装openssl
--yum--
$ yum install openssl-devel libssl-devel
--or apt-get--
$ apt-get install libssl-dev
Run Code Online (Sandbox Code Playgroud)
和pyopenssl通过easy install/pip
--easy install--
$ easy_install pyopenssl
--or pip--
$ pip install pyopenssl
Run Code Online (Sandbox Code Playgroud)
小智 6
gcloud auth activate-service-account \
test-service-account@google.com \
--key-file=/path/key.json \
--project=testproject
Run Code Online (Sandbox Code Playgroud)
# ex: bq query
bq query --use_legacy_sql=false 'SELECT CURRENT_DATE()'
Run Code Online (Sandbox Code Playgroud)
gcloud config set account you@google.com
Run Code Online (Sandbox Code Playgroud)
gcloud auth list
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
5944 次 |
| 最近记录: |