0
votes

I want to access BigQuery to select data from table in my JAVA application. Firstly, I have created a service account and gave a permission as BigQuery Admin. Json of service account was passed as an environment variable, I used the code as below(got it from https://cloud.google.com/docs/authentication/production)

static void authImplicit() {
  // If you don't specify credentials when constructing the client, the client library will
  // look for credentials via the environment variable GOOGLE_APPLICATION_CREDENTIALS.
  Storage storage = StorageOptions.getDefaultInstance().getService();

  System.out.println("Buckets:");
  Page<Bucket> buckets = storage.list();
  for (Bucket bucket : buckets.iterateAll()) {
    System.out.println(bucket.toString());
  }
}

The method returns 401 with this error response:

Caused by: com.google.api.client.googleapis.json.GoogleJsonResponseException: 401 Unauthorized { "code" : 401, "errors" : [ { "domain" : "global", "location" : "Authorization", "locationType" : "header", "message" : "Login Required.", "reason" : "required" } ], "message" : "Request is missing required authentication credential. Expected OAuth 2 access token, login cookie or other valid authentication credential. See https://developers.google.com/identity/sign-in/web/devconsole-project.", "status" : "UNAUTHENTICATED" }

1
Can you please share more details to reproduce this error? Are you connecting from an external Java app or from an app hosted in App Engine? are you setting the Environmental Variable as stated in the documentation?Chris32
Hi @Chris32, yes it is external java app running on my local computer. Later, I will put my app on compute engine. but for the first step, I run impicit() method like in this code [github.com/googleapis/java-bigquery/blob/master/samples/src/… with passing env variable.Denis

1 Answers

0
votes

From your description looks like you are not placing your json file in the described path in your environmental variable. This is assuming that you are describing correctly your project id in the properties file, you have enabled the required APIs in Google Cloud Platform. In order to determine this with more precision I would need you to share with us your folder structure and your properties files.

I would suggest you to use the CredentialsProvider interface to solve this issue

public interface CredentialsProvider {
  Credentials getCredentials() throws IOException;
}

You can find more information on this interface here.

You can also take a look of this quickstart made for spring + bigquery from the Spring team