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

이 빠른 시작 가이드의 단계를 따르면 약 10분 만에 제로터치에 요청을 보내는 간단한 Java 명령줄 앱 등록 리셀러 API

기본 요건

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

  • 제로터치 등록 리셀러의 Google 계정 있습니다. 아직 온보딩하지 않았다면 다음에서 시작하기 단계를 따르세요. 리셀러 포털 가이드를 참조하세요.
  • Java 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 호출을 승인하는 방법을 알아보려면 승인.

자세히 알아보기