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

## Prerequisites

To run this quickstart, you'll need:

- A Google account, that's a member of your zero-touch enrollment reseller account. If you haven't onboarded yet, follow the steps in *Get started* in the [Reseller portal guide](https://developers.google.com/zero-touch/guides/portal).
- Java 1.7 or greater.
- [Gradle 2.3 or greater](http://gradle.org/downloads).
- Access to the internet and a web browser.

## Step 1: Turn on the zero-touch enrollment API

1. Use [this
   wizard](https://console.developers.google.com/start/api?id=androiddeviceprovisioning.googleapis.com) 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`.

> [!WARNING]
> **Warning:** Service account keys can become a security risk if not managed carefully. For advice see [best practices for managing API keys](https://cloud.google.com/iam/docs/best-practices-for-managing-service-account-keys).

## Step 2: Link the service account

1. Open the zero-touch enrollment [portal](https://enterprise.google.com/android/zero-touch/resellers). You might need to sign in.
2. Click **Service
   accounts**.
3. Click **Link service account**.
4. Set **Email address** to the address of the service account you created.
5. Click **Link service account** to use the service account with your zero-touch enrollment account.

## Step 3: 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` file you downloaded in Step 1 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 = 'ResellerQuickstart'
       sourceCompatibility = 1.7
       targetCompatibility = 1.7
       version = '1.0'

       repositories {
           mavenCentral()
       }

       dependencies {
           compile 'com.google.api-client:google-api-client:1.30.11'
           compile 'com.google.apis:google-api-services-androiddeviceprovisioning:+'
           compile 'com.google.oauth-client:google-oauth-client-jetty:+'
       }

## Step 4: Set up the sample

Create a file named `src/main/java/ResellerQuickstart.java` and copy in the
following code and save the file. Insert your own [reseller partner
ID](https://developers.google.com/zero-touch/guides/quickstart/java#partner-id) as the value for `PARTNER_ID` (the app's first line).

```java
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.JsonFactory;
import com.google.api.client.json.jackson2.JacksonFactory;
import com.google.api.services.androiddeviceprovisioning.v1.AndroidProvisioningPartner;
import com.google.api.services.androiddeviceprovisioning.v1.model.Company;
import com.google.api.services.androiddeviceprovisioning.v1.model.ListCustomersResponse;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.Arrays;
import java.util.List;

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

  // TODO: replace this with your partner reseller ID.
  private static long PARTNER_ID = 11036885;

  // Use a single scope for the all methods in the reseller API.
  private static final List<String> SCOPES =
      Arrays.asList("https://www.googleapis.com/auth/androidworkprovisioning");
  private static final String APP_NAME = "Zero-touch Reseller Java Quickstart";

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

  static {
    try {
      HTTP_TRANSPORT = GoogleNetHttpTransport.newTrustedTransport();
    } catch (Throwable t) {
      t.printStackTrace();
      System.exit(1);
    }
  }

  /**
   * Creates a Credential 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 Credential object.
   * @throws IOException
   */
  public static Credential authorize() throws IOException {
      // Load the service account key from the JSON file.
      InputStream in =
          ResellerQuickstart.class.getResourceAsStream("/service_account_key.json");

      // Create the credential scoped to the zero-touch enrollemnt
      // reseller APIs.
      GoogleCredential credential = GoogleCredential
         .fromStream(in)
         .createScoped(SCOPES);
      return credential;
  }

  /**
   * Builds and returns 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 {
    Credential credential = authorize();
    return new AndroidProvisioningPartner.Builder(
        HTTP_TRANSPORT, JSON_FACTORY, credential)
        .setApplicationName(APP_NAME)
        .build();
  }

  /**
   * 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();

    // Send an API request to list all our customers.
    AndroidProvisioningPartner.Partners.Customers.List request =
          service.partners().customers().list(PARTNER_ID);
    ListCustomersResponse response = request.execute();

    // Print out the details of each customer.
    if (response.getCustomers() != null) {
      java.util.List<Company> customers = response.getCustomers();
      for (Company customer : customers) {
          System.out.format("Name:%s  ID:%d\n",
                customer.getCompanyName(),
                customer.getCompanyId());
      }
    } else {
      System.out.println("No customers found");
    }
  }
}
```

### Partner ID

API calls typically need your reseller partner ID as an argument. To find your
partner ID from the zero-touch enrollment portal, follow the steps below:

1. Open the [portal](https://enterprise.google.com/android/zero-touch/resellers). You might need to sign in.
2. Click **Service
   accounts**.
3. Copy your partner ID number from the **Your reseller ID** line.

## Step 5: 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

## Troubleshooting


[Tell us what went wrong]() with the quickstart and we'll work to
fix it. To learn how zero-touch uses service accounts to authorize API calls, read
[Authorization](https://developers.google.com/zero-touch/guides/auth).

## Learn more

- [Google Developers Console help documentation](https://developers.google.com/console/help/new)
- [Google APIs Client for Java documentation](https://developers.google.com/api-client-library/java)
- [Google APIs Client Javadocs](https://developers.google.com/api-client-library/java/google-api-java-client/reference)