2012-11-06 115 views
8

我已經使用Google API控制檯創建了一個服務帳戶,並希望將此服務帳戶與Google BigQuery CLI(bq)工具結合使用。如何配置Google BigQuery命令行工具以使用服務帳戶?

我一直在使用命令行工具在〜/ .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 paramater到我的服務帳戶.p12私鑰文件,但沒有運氣,它給我回以下錯誤

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

並要求我轉到瀏覽器中的鏈接以再次設置我的OAuth2憑據。

命令行工具的初始配置選項‘初始化’:

bq help init 

顯示有關如何設置這個工具來使用服務帳戶沒有有用的信息。

回答

5

我最終找到了如何設置此

$ 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的命令行客戶端)的請求,即一些文檔:

$ 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 = [email protected] 
service_account_credential_file = /home/james/.bigquery.v2.cred 
service_account_private_key_file = /home/james/.bigquery.v2.p12 

服務accou nt可以在Google API控制檯中找到,並且您在創建服務帳戶時設置service_account_private_key_password(默認爲「notasecret」)。

注:在的.bigqueryrc文件路徑必須是完整路徑,我無法使用〜/ .bigquery ......被要求

一些額外的依賴,你需要通過安裝OpenSSL的百勝/ apt-get的

--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 
+0

以下使用Docker鏡像服務帳戶的說明至少在OS X上很容易:https://hub.docker.com/r/google/cloud-sdk/ – mentat

+0

正確使用服務帳戶憑證的方式在bq cli上是使用「gcloud auth activate-service-account」命令激活它,然後在沒有任何auth標誌的情況下運行bq。這些bq標誌僅供gcloud內部使用。請不要設置它們,它會干擾gcloud並導致不可預測的結果。 – Daria

1

的BQ工具需要兩個配置文件,由--bigqueryrc和--credential_file標誌控制。如果找不到,bq將嘗試在啓動時自動初始化。

要避免使用--bigqueryrc文件,可以在默認位置放置一個「.bigqueryrc」文件,或者用--bigqueryrc將其覆蓋到某個可寫文件路徑。

相關問題