Home > Cloud Computing, Software Development > Google Cloud Platform: Hello World with Service Account

Google Cloud Platform: Hello World with Service Account

March 21st, 2016 Leave a comment Go to comments

In my last post, I showed a sample with OAuth. It works well for application on others’ behalves. For the hello world program, it’s better to run without any user’s manual interaction. Google has provided “Service Account Key” for that purpose.

When you create a new credential, the console asks the type of key. You can select the service account key, and then select the “Compute engine default service account” with P12 as the key type. The generated key will be downloaded automatically. Save it safely and it cannot download again. You can then copy the key to the project home with a name as key.p12.

Lost VMs or Containers? Too Many Consoles? Too Slow GUI? Time to learn how to "Google" and manage your VMware and clouds in a fast and secure HTML5 App.

For the service account id, I did not find it anywhere on the console GUI. But luckily I found one from the OAuth JSON file. Just copy it over.

package org.doublecloud.googleclouddemo;
import com.google.api.client.auth.oauth2.Credential;
import com.google.api.client.googleapis.auth.oauth2.GoogleCredential;
import com.google.api.client.googleapis.javanet.GoogleNetHttpTransport;
import com.google.api.client.http.HttpTransport;
import com.google.api.client.json.jackson2.JacksonFactory;
import com.google.api.services.compute.Compute;
import com.google.api.services.compute.ComputeScopes;
import com.google.api.services.compute.model.Disk;
import com.google.api.services.compute.model.DiskList;
import com.google.api.services.compute.model.Instance;
import com.google.api.services.compute.model.InstanceList;
import com.google.api.services.compute.model.Network;
import com.google.api.services.compute.model.NetworkList;
import com.google.api.services.compute.model.Project;
import java.io.File;
import java.io.IOException;
import java.security.GeneralSecurityException;
import java.util.Arrays;
import java.util.List;
public class ComputeEngineServiceAccount
  private static final String APPLICATION_NAME = "doublecloud/1.0";
  /** project name must be lower case even you see ComputeEngineDemo on GUI */
  private static final String projectId = "computeenginedemo";
  private static final List<String> SCOPES = Arrays.asList(ComputeScopes.COMPUTE);
  public static void main(String[] args) throws IOException, GeneralSecurityException, Exception
    HttpTransport httpTransport = GoogleNetHttpTransport.newTrustedTransport();
    // Authorize
    Credential credential = new GoogleCredential.Builder()
      .setServiceAccountPrivateKeyFromP12File(new File("key.p12"))
    // Create compute engine object
    Compute compute = new Compute.Builder(httpTransport, JacksonFactory.getDefaultInstance(), null)
    Project proj = compute.projects().get(projectId).execute();
    Compute.Instances.List instances = compute.instances().list(projectId, "us-central1-a");
    InstanceList list = instances.execute();
    for (Instance instance : list.getItems())

The program runs by itself and you will see output similar to that of last program using OAuth. Therefore, the output is omitted here.

  1. April 15th, 2016 at 09:02 | #1

    Thanks for this guys – I will be sure to try a few of these out and I will be sharing – Great work.

  1. No trackbacks yet.