Passing Service Account Credential(JSON File) programmatically for BigQuery Service

Viewed 2395

I am exporting data from BigQuery table into Google Cloud Storage and I am able to do this successfully by using Application Default Credentials. I have set the environment variable GOOGLE_APPLICATION_CREDENTIALS in bash_profile(This environment variable points to my Service account JSON file) and this is how I am able to retrieve BigQuery Service(I am using Scala(Java API is used)).

val bigquery: BigQuery = BigQueryOptions.getDefaultInstance().getService()

Now, I am trying to find a way to retrieve the BigQuery service by passing the service account JSON file programmatically instead of using the environment variable(i.e. GOOGLE_APPLICATION_CREDENTIALS)

val inputStream = this.getClass.getResourceAsStream("/GCCredentials.json")
val credentials = GoogleCredentials.fromStream(inputStream)
val bigquery = BigQueryOptions.newBuilder().setCredentials(credentials).build().getService    

But this code do not work and I am getting the following exception

Exception in thread "main" java.lang.NullPointerException
    at com.google.api.client.repackaged.com.google.common.base.Preconditions.checkNotNull(Preconditions.java:213)
    at com.google.api.client.util.Preconditions.checkNotNull(Preconditions.java:127)

What am I missing in my code?

1 Answers
Related