4444
4545
4646def get_client (project_id , credentials = None , service_account = None ,
47- private_key = None , private_key_file = None , readonly = True ,
48- swallow_results = True ):
47+ private_key = None , private_key_file = None ,
48+ json_key = None , json_key_file = None ,
49+ readonly = True , swallow_results = True ):
4950 """Return a singleton instance of BigQueryClient. Either
5051 AssertionCredentials or a service account and private key combination need
5152 to be provided in order to authenticate requests to BigQuery.
@@ -60,6 +61,9 @@ def get_client(project_id, credentials=None, service_account=None,
6061 private_key_file: the name of the file containing the private key
6162 associated with the service account in PKCS12 or PEM
6263 format.
64+ json_key: the JSON key associated with the service account
65+ json_key_file: the name of the JSON key file associated with
66+ the service account
6367 readonly: bool indicating if BigQuery access is read-only. Has no
6468 effect if credentials are provided.
6569 swallow_results: If set to false then return the actual response value
@@ -70,13 +74,21 @@ def get_client(project_id, credentials=None, service_account=None,
7074 """
7175
7276 if not credentials :
73- assert service_account and (private_key or private_key_file ), \
74- 'Must provide AssertionCredentials or service account and key'
77+ assert ( service_account and (private_key or private_key_file )) or ( json_key or json_key_file ), \
78+ 'Must provide AssertionCredentials or service account and P12 key or JSON key'
7579
7680 if private_key_file :
7781 with open (private_key_file , 'rb' ) as key_file :
7882 private_key = key_file .read ()
7983
84+ if json_key_file :
85+ with open (json_key_file , 'rb' ) as key_file :
86+ json_key = json .loads (key_file .read ())
87+
88+ if json_key :
89+ service_account = json_key ['client_email' ]
90+ private_key = json_key ['private_key' ]
91+
8092 bq_service = _get_bq_service (credentials = credentials ,
8193 service_account = service_account ,
8294 private_key = private_key ,
0 commit comments