@@ -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