Java quickstart for customers using a service account

Follow the steps in this quickstart guide, and in about 10 minutes you have a simple Java command-line app that makes requests to the zero-touch enrollment customer API using a service account.


To run this quickstart, you need:

  • A service account, that's linked to you zero-touch enrollment customer account. See Get started.
  • Java 1.7 or greater.
  • Gradle 2.3 or greater.
  • Access to the internet and a web browser.

Step 1: Turn on the zero-touch enrollment API

  1. Use this wizard to create or select a project in the Google Developers Console and automatically turn on the API. Click Continue, then Go to credentials .
  2. Set What data will you be accessing? to Application data.
  3. Click Next. You should be prompted to create a service account.
  4. Give a descriptive name for Service account name.
  5. Note the Service account ID (it looks like an email address) because you'll use it later.
  6. Set Role to Service Accounts > Service Account User.
  7. Click Done to finish creating the service account.
  8. Click the email address for the service account that you created.
  9. Click **Keys**.
  10. Click **Add key**, then click **Create new key**.
  11. For **Key type**, select **JSON**.
  12. Click Create and the private key downloads to your computer.
  13. Click **Close**.
  14. Move the file to your working directory and rename it service_account_key.json.

Step 2: Prepare the project

Follow the steps below to set up your Gradle project:

  1. Run the following command to create a new project in the working directory:

    gradle init --type basic
    mkdir -p src/main/java src/main/resources
  2. Copy the service_account_key.json you downloaded when you created your service account into the src/main/resources/ directory you created above.

  3. Open the default build.gradle file and replace its contents with the following code:

apply plugin: 'java'
apply plugin: 'application'

mainClassName = 'CustomerQuickstart'
sourceCompatibility = 1.7
targetCompatibility = 1.7
version = '1.0'

repositories {

dependencies {
    compile ''
    compile ''
    compile ''
    compile ''
    compile ''
    compile ''

Step 3: Set up the sample

Create a file named src/main/java/ and copy in the following code and save the file.

import java.util.Arrays;
import java.util.List;

/** This class forms the quickstart introduction to the zero-touch enrollment customer API. */
public class CustomerQuickstart {

  // A single auth scope is used for the zero-touch enrollment customer API.
  private static final List<String> SCOPES =
  private static final String APP_NAME = "Zero-touch Enrollment Java Quickstart";

  // Global shared instances
  private static final JsonFactory JSON_FACTORY = GsonFactory.getDefaultInstance();
  private static HttpTransport HTTP_TRANSPORT;

  static {
    try {
      HTTP_TRANSPORT = GoogleNetHttpTransport.newTrustedTransport();
    } catch (Throwable t) {

   * Creates a GoogleCredentials object with the correct OAuth2 authorization for the service
   * account that calls the reseller API. The service endpoint invokes this method when setting up a
   * new service instance.
   * @return an authorized GoogleCredentials object.
   * @throws IOException
  public static GoogleCredentials authorize() throws IOException {
    // Load service account key.
    InputStream in = CustomerQuickstart.class.getResourceAsStream("/service_account_key.json");

    // Create the credential scoped to the zero-touch enrollment customer APIs.
    GoogleCredentials credential = ServiceAccountCredentials.fromStream(in).createScoped(SCOPES);
    return credential;

   * Build and return an authorized zero-touch enrollment API client service. Use the service
   * endpoint to call the API methods.
   * @return an authorized client service endpoint
   * @throws IOException
  public static AndroidProvisioningPartner getService() throws IOException {
    GoogleCredentials credential = authorize();
    HttpRequestInitializer requestInitializer = new HttpCredentialsAdapter(credential);
    return new AndroidProvisioningPartner.Builder(HTTP_TRANSPORT, JSON_FACTORY, requestInitializer)

   * Runs the zero-touch enrollment quickstart app.
   * @throws IOException
  public static void main(String[] args) throws IOException {

    // Create a zero-touch enrollment API service endpoint.
    AndroidProvisioningPartner service = getService();

    // Get the customer's account. Because a customer might have more
    // than one, limit the results to the first account found.
    AndroidProvisioningPartner.Customers.List accountRequest = service.customers().list();
    CustomerListCustomersResponse accountResponse = accountRequest.execute();
    if (accountResponse.getCustomers().isEmpty()) {
      // No accounts found for the user. Confirm the Google Account
      // that authorizes the request can access the zero-touch portal.
      System.out.println("No zero-touch enrollment account found.");
    Company customer = accountResponse.getCustomers().get(0);
    String customerAccount = customer.getName();

    // Send an API request to list all the DPCs available using the customer account.
    AndroidProvisioningPartner.Customers.Dpcs.List request =
    CustomerListDpcsResponse response = request.execute();

    // Print out the details of each DPC.
    java.util.List<Dpc> dpcs = response.getDpcs();
    for (Dpc dpcApp : dpcs) {
      System.out.format("Name:%s  APK:%s\n", dpcApp.getDpcName(), dpcApp.getPackageName());

Step 4: Run the sample

Use your operating system's help to run the script in the file. On UNIX and Mac computers, run the command below in your terminal:

gradle -q run


  • Avoid sharing your service_account_key.json file with anyone. Be careful not to include it in source code repositories. You can read more advice on handling service account secrets.

Learn more