經銷商適用的 Java 快速入門導覽課程

請按照這份快速入門指南中的步驟進行,大約 10 分鐘後 簡單的 Java 指令列應用程式,向零接觸 。

必要條件

如要執行本快速入門導覽課程,您需要:

步驟 1:啟用零接觸註冊機制 API

  1. 請使用此 精靈在 Google Developers Console 中建立或選取專案,並且 系統會自動啟用 API依序點選「繼續」和「前往憑證」
  2. 將「您要存取哪些資料?」設為「應用程式資料」
  3. 按一下「繼續」,系統會提示您建立服務 讓他們使用服務帳戶
  4. 為「服務帳戶名稱」設定描述性的名稱。
  5. 記下「服務帳戶 ID」 (形式為電子郵件地址), 以便稍後使用
  6. 角色設為服務帳戶 >服務帳戶使用者
  7. 按一下「Done」(完成),即完成建立服務帳戶。
  8. 點選您建立的服務帳戶的電子郵件地址。
  9. 按一下「鍵」。
  10. 依序按一下「新增金鑰」和「建立新的金鑰」。
  11. 在「金鑰類型」部分選取「JSON」。
  12. 按一下「建立」,私密金鑰就會下載到您的電腦。
  13. 按一下「關閉」。
  14. 將檔案移至工作目錄,並重新命名為 service_account_key.json
  1. 開啟零接觸註冊機制入口網站。您可能需要登入。
  2. 按一下 服務 帳戶
  3. 按一下「連結服務帳戶」
  4. 將「Email address」(電子郵件地址) 設為您建立的服務帳戶的電子郵件地址。
  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 的檔案,然後複製至 下列程式碼並儲存檔案。插入自己的經銷商合作夥伴 ID 做為 PARTNER_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 呼叫,請參閱 授權

瞭解詳情