GCP API Client Libraries Java Examples - com.google.api.services.deploymentmanager.DeploymentManager

I am looking to automate the provisioning of google cloud resources using Deployment Manager. Only I want to use the JAVA API library instead of gcloud.

Specifically need to provision the following components using DeploymentManager.

  1. Cloud SQL - MSSQL
  2. Cloud Run Service
  3. VPC Network with the Connector to be used to connect Cloud Run to the Cloud SQL.

I think com.google.api.services.deploymentmanager.DeploymentManager is the starting point but I am not very sure.

This is what I have so far


        Builder builder = new Builder(
                com.google.api.client.googleapis.javanet.GoogleNetHttpTransport.newTrustedTransport(),
                new com.google.api.client.json.gson.GsonFactory(), null);
        builder.setApplicationName("spatial-cat-319209");
        builder.setDeploymentManagerRequestInitializer(new DeploymentManagerRequestInitializer());
        DeploymentManager deploymentManager = builder.build();

        deploymentManager.deployments().list("spatial-cat-319209").entrySet().stream().forEach(entry -> {
            System.out.println(entry.getKey() + " = " + entry.getValue());
        });
        
        System.out.println(System.getProperty("GOOGLE_APPLICATION_CREDENTIALS"));
        System.out.println(System.getenv("GOOGLE_APPLICATION_CREDENTIALS"));
        
        deploymentManager.deployments().list("spatial-cat-319209").execute();

I get the following error in the last line:

Exception in thread "main" com.google.api.client.googleapis.json.GoogleJsonResponseException: 401 Unauthorized
GET https://deploymentmanager.googleapis.com/deploymentmanager/v2/projects/spatial-cat-319209/global/deployments
{
  "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"
}
    at com.google.api.client.googleapis.json.GoogleJsonResponseException.from(GoogleJsonResponseException.java:146)
    at com.google.api.client.googleapis.services.json.AbstractGoogleJsonClientRequest.newExceptionOnError(AbstractGoogleJsonClientRequest.java:118)
    at com.google.api.client.googleapis.services.json.AbstractGoogleJsonClientRequest.newExceptionOnError(AbstractGoogleJsonClientRequest.java:37)
    at com.google.api.client.googleapis.services.AbstractGoogleClientRequest$1.interceptResponse(AbstractGoogleClientRequest.java:428)
    at com.google.api.client.http.HttpRequest.execute(HttpRequest.java:1111)
    at com.google.api.client.googleapis.services.AbstractGoogleClientRequest.executeUnparsed(AbstractGoogleClientRequest.java:514)
    at com.google.api.client.googleapis.services.AbstractGoogleClientRequest.executeUnparsed(AbstractGoogleClientRequest.java:455)
    at com.google.api.client.googleapis.services.AbstractGoogleClientRequest.execute(AbstractGoogleClientRequest.java:565)

I do have the environment variable GOOGLE_APPLICATION_CREDENTIALS pointing to a file that contains the service account key (generated using gcloud auth application-default login command)

Any insights would be very helpful.

How many English words
do you know?
Test your English vocabulary size, and measure
how many words do you know
Online Test
Powered by Examplum