Skip to content

Commit e5bb214

Browse files
committed
Merge pull request abronte#42 from takeyuweb/json_key
Support JSON Key file authentication
2 parents d4bf896 + 3f0686d commit e5bb214

File tree

3 files changed

+27
-12
lines changed

3 files changed

+27
-12
lines changed

Gemfile.lock

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ PATH
33
specs:
44
bigquery (0.9.0)
55
google-api-client (= 0.9.3)
6+
googleauth (>= 0.5.0)
67

78
GEM
89
remote: https://rubygems.org/
@@ -36,9 +37,9 @@ GEM
3637
signet (~> 0.7)
3738
httpclient (2.7.1)
3839
hurley (0.2)
39-
jwt (1.5.3)
40+
jwt (1.5.4)
4041
little-plugger (1.1.4)
41-
logging (2.0.0)
42+
logging (2.1.0)
4243
little-plugger (~> 1.1)
4344
multi_json (~> 1.10)
4445
memoist (0.14.0)

bigquery.gemspec

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ Gem::Specification.new do |s|
1616
s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
1717

1818
s.add_dependency "google-api-client", "0.9.3"
19+
s.add_dependency "googleauth", ">= 0.5.0"
1920

2021
s.add_development_dependency "bundler"
2122
s.add_development_dependency "rake"

lib/big_query/client.rb

Lines changed: 23 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -47,18 +47,31 @@ def initialize(opts = {})
4747
@client.request_options.open_timeout_sec = opts['request_option']['open_timeout_sec']
4848
end
4949

50-
begin
51-
key = Google::APIClient::KeyUtils.load_from_pkcs12(opts['key'], 'notasecret')
52-
rescue ArgumentError
53-
key = Google::APIClient::KeyUtils.load_from_pem(opts['key'], 'notasecret')
50+
scope = 'https://www.googleapis.com/auth/bigquery'
51+
if opts['json_key'].is_a?(String) && !opts['json_key'].empty?
52+
if File.exist?(opts['json_key'])
53+
auth = File.open(opts['json_key']) do |f|
54+
Google::Auth::ServiceAccountCredentials.make_creds(json_key_io: f, scope: scope)
55+
end
56+
else
57+
key = StringIO.new(opts['json_key'])
58+
auth = Google::Auth::ServiceAccountCredentials.make_creds(json_key_io: key, scope: scope)
59+
end
60+
else
61+
begin
62+
key = Google::APIClient::KeyUtils.load_from_pkcs12(opts['key'], 'notasecret')
63+
rescue ArgumentError
64+
key = Google::APIClient::KeyUtils.load_from_pem(opts['key'], 'notasecret')
65+
end
66+
auth = Signet::OAuth2::Client.new(
67+
token_credential_uri: 'https://accounts.google.com/o/oauth2/token',
68+
audience: 'https://accounts.google.com/o/oauth2/token',
69+
scope: scope,
70+
issuer: opts['service_email'],
71+
signing_key: key)
5472
end
5573

56-
@client.authorization = Signet::OAuth2::Client.new(
57-
token_credential_uri: 'https://accounts.google.com/o/oauth2/token',
58-
audience: 'https://accounts.google.com/o/oauth2/token',
59-
scope: 'https://www.googleapis.com/auth/bigquery',
60-
issuer: opts['service_email'],
61-
signing_key: key)
74+
@client.authorization = auth
6275

6376
refresh_auth
6477

0 commit comments

Comments
 (0)