리셀러를 위한 자바 빠른 시작

이 빠른 시작 가이드의 단계를 따르면 약 10분 후에 제로터치 등록 리셀러 API에 요청하는 간단한 자바 명령줄 앱을 사용할 수 있습니다.

기본 요건

이 빠른 시작을 실행하려면 다음이 필요합니다.

  • 제로터치 등록 리셀러 계정의 구성원인 Google 계정 아직 온보딩하지 않은 경우 리셀러 포털 가이드시작하기 단계를 따릅니다.
  • 자바 1.7 이상
  • Gradle 2.3 이상.
  • 인터넷 및 웹브라우저 액세스

1단계: 제로터치 등록 API 사용 설정하기

  1. 이 마법사를 사용하여 Google Developers Console에서 프로젝트를 만들거나 선택하고 API를 자동으로 사용 설정하세요. 계속을 클릭한 다음 사용자 인증 정보로 이동을 클릭합니다.
  2. 어떤 데이터에 액세스하시겠습니까?애플리케이션 데이터로 설정합니다.
  3. 다음을 클릭합니다. 서비스 계정을 만들라는 메시지가 표시됩니다.
  4. 서비스 계정 이름을 설명하는 이름을 입력합니다.
  5. 나중에 사용할 수 있도록 서비스 계정 ID (이메일 주소 모양)를 기록해 둡니다.
  6. 역할서비스 계정 > 서비스 계정 사용자로 설정합니다.
  7. 완료를 클릭하여 서비스 계정 만들기를 마칩니다.
  8. 만든 서비스 계정의 이메일 주소를 클릭합니다.
  9. **키**를 클릭합니다.
  10. **키 추가**를 클릭한 다음 **새 키 만들기**를 클릭합니다.
  11. **키 유형**에서 **JSON**을 선택합니다.
  12. 만들기를 클릭하면 비공개 키가 컴퓨터에 다운로드됩니다.
  13. **닫기**를 클릭합니다.
  14. 파일을 작업 디렉터리로 이동하고 이름을 service_account_key.json로 변경합니다.
  1. 제로터치 등록 포털을 엽니다. 로그인해야 할 수도 있습니다.
  2. 서비스 계정을 클릭합니다.
  3. 서비스 계정 연결을 클릭합니다.
  4. 이메일 주소를 앞서 만든 서비스 계정의 주소로 설정합니다.
  5. 서비스 계정 연결을 클릭하여 제로터치 등록 계정으로 서비스 계정을 사용합니다.

3단계: 프로젝트 준비

Gradle 프로젝트를 설정하려면 아래 단계를 따르세요.

  1. 다음 명령어를 실행하여 작업 디렉터리에 새 프로젝트를 만듭니다.

    gradle init --type basic
    mkdir -p src/main/java src/main/resources
    
  2. 1단계에서 다운로드한 service_account_key.json 파일을 위에서 만든 src/main/resources/ 디렉터리에 복사합니다.

  3. 기본 build.gradle 파일을 열고 콘텐츠를 다음 코드로 바꿉니다.

    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:+'
    }
    

4단계: 샘플 설정

이름이 src/main/java/ResellerQuickstart.java인 파일을 만들고 다음 코드를 복사하여 파일을 저장합니다. 자체 리셀러 파트너 IDPARTNER_ID(앱의 첫 번째 줄) 값으로 삽입합니다.

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 enrollemnt
 * 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");
    }
  }
}

파트너 ID

API 호출에는 일반적으로 리셀러 파트너 ID가 인수로 필요합니다. 제로터치 등록 포털에서 파트너 ID를 찾으려면 다음 단계를 따르세요.

  1. 포털을 엽니다. 로그인해야 할 수도 있습니다.
  2. 서비스 계정을 클릭합니다.
  3. 리셀러 ID 행에서 파트너 ID 번호를 복사합니다.

5단계: 샘플 실행

운영체제의 도움말을 사용하여 파일에서 스크립트를 실행합니다. UNIX 및 Mac 컴퓨터의 경우 터미널에서 아래 명령어를 실행합니다.

gradle -q run

문제 해결

빠른 시작에 어떤 문제가 발생했는지 알려주시면 해결하도록 노력하겠습니다. 제로터치가 서비스 계정을 사용하여 API 호출을 승인하는 방법을 알아보려면 승인을 읽어보세요.

자세히 알아보기