From cf99bd6728bd2e12b38b4b52fcfd8b1cd8ca35c3 Mon Sep 17 00:00:00 2001 From: yuemori Date: Sat, 31 Oct 2015 10:33:32 +0900 Subject: [PATCH] Support installed application credentials --- README.md | 17 +++++++++++++++++ lib/bigquery-client/client.rb | 15 +++++++++++++++ 2 files changed, 32 insertions(+) diff --git a/README.md b/README.md index b4db9da..3895ffc 100644 --- a/README.md +++ b/README.md @@ -11,6 +11,7 @@ A Ruby interface to the BigQuery API. ``` ## Getting Started +### Use private_key ```ruby require "bigquery-client" @@ -27,6 +28,22 @@ client = BigQuery::Client.new( client.sql "SELECT * FROM publicdata:samples.wikipedia LIMIT 10" ``` +### Use client_secrets + +```ruby +require "bigquery-client" + +client = BigQuery::Client.new( + project: "your-project-42", + dataset: "your_dataset", + client_secrets_path: "/path/to/client_secrets.json", + credential_store_file: "/path/to/credential_store_file.json", + auth_method: "client_secrets" +) + +client.sql "SELECT * FROM publicdata:samples.wikipedia LIMIT 10" +``` + ## Available API methods https://cloud.google.com/bigquery/docs/reference/v2/ diff --git a/lib/bigquery-client/client.rb b/lib/bigquery-client/client.rb index 8aecb39..3b5a4a7 100644 --- a/lib/bigquery-client/client.rb +++ b/lib/bigquery-client/client.rb @@ -74,6 +74,21 @@ def authorize_client auth = Google::APIClient::ComputeServiceAccount.new auth.fetch_access_token! @client.authorization = auth + when 'client_secrets' + file_storage = Google::APIClient::Storage.new(Google::APIClient::FileStore.new(@credential_store_file)) + auth = file_storage.authorize + + if file_storage.authorization.nil? + client_secrets = Google::APIClient::ClientSecrets.load(@client_secrets_path) + flow = Google::APIClient::InstalledAppFlow.new( + client_id: client_secrets.client_id, + client_secret: client_secrets.client_secret, + scope: 'https://www.googleapis.com/auth/bigquery' + ) + + auth = flow.authorize(file_storage) + end + @client.authorization = auth end end end