【问题标题】:How do I configure Google BigQuery command line tool to use a Service Account?如何配置 Google BigQuery 命令行工具以使用服务帐户?
【发布时间】:2012-10-26 14:37:31
【问题描述】:

我已使用 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"
}

我的另一个文件:~/.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 init

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

【问题讨论】:

    标签: google-bigquery


    【解决方案1】:

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

    $ 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
    

    服务帐户可以在 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
    

    和 pyopenssl 通过简单的安装/pip

    --easy install--
    $ easy_install pyopenssl
    
    --or pip--
    $ pip install pyopenssl
    

    【讨论】:

    • 至少在 OS X 上,遵循使用 docker 映像进行服务帐户的说明要容易得多:hub.docker.com/r/google/cloud-sdk
    • 在 bq cli 上使用服务帐户凭据的正确方法是使用“gcloud auth activate-service-account”命令激活它,然后在没有任何身份验证标志的情况下运行 bq。这些 bq 标志仅供 gcloud 内部使用。请不要设置它们,它会干扰 gcloud 并导致无法预料的结果。
    【解决方案2】:

    bq 授权标志现已弃用

    bq documentation

    【讨论】:

      【解决方案3】:

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

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

      【讨论】:

        【解决方案4】:

        1.) Tell gcloud to authenticate as your service account

        gcloud auth activate-service-account \
        test-service-account@google.com \
        --key-file=/path/key.json \
        --project=testproject
        

        2.) Run a bq command as you would with your user account

        # ex: bq query
        bq query --use_legacy_sql=false 'SELECT CURRENT_DATE()'
        

        3. optional) Revert gcloud authentication to your user account

        gcloud config set account you@google.com
        

        3a. optional) See who gcloud uses for authentication

        gcloud auth list
        

        【讨论】:

          猜你喜欢
          • 2019-05-20
          • 1970-01-01
          • 2014-01-04
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2014-08-17
          • 1970-01-01
          相关资源
          最近更新 更多