請按照本快速入門指南中的步驟操作,而且在 10 分鐘內會有簡單的 Java 指令列應用程式,透過服務帳戶向零接觸註冊客戶客戶要求傳送要求。
必備條件
如要執行本快速入門導覽課程,您需要:
- 連結至您的零接觸註冊機制客戶帳戶的服務帳戶。請參閱開始使用。
- Java 1.7 以上版本。
- Gradle 2.3 以上版本。
- 連線至網際網路和網路瀏覽器。
步驟 1:啟用零接觸註冊 API
- 使用這個精靈在 Google Developers Console 中建立或選取專案,然後自動啟用 API。依序點選「繼續」和「前往憑證」。
- 將「您要存取哪些資料?」設為「應用程式資料」。
- 點選「下一步」。系統會提示您建立服務帳戶。
- 為「服務帳戶名稱」輸入描述性名稱。
- 請記下服務帳戶 ID (與電子郵件地址相似),以便稍後使用。
- 將「角色」設為「服務帳戶」>「服務帳戶使用者」。
- 按一下「Done」(完成),即完成建立服務帳戶。
- 按一下您建立的服務帳戶電子郵件地址。
- 按一下「金鑰」。
- 點選「新增金鑰」,然後點選「建立新的金鑰」。
- 在「金鑰類型」部分選取 [JSON]。
- 按一下「Create」(建立),並將私密金鑰下載到電腦上。
- 點選「關閉」。
- 將檔案移至工作目錄,並重新命名為
service_account_key.json
。
步驟 2:準備專案
請按照下列步驟設定 Gradle 專案:
執行下列指令,在工作目錄中建立新專案:
gradle init --type basic mkdir -p src/main/java src/main/resources
將您在建立服務帳戶時下載的
service_account_key.json
複製到您在上述步驟中建立的src/main/resources/
目錄。開啟預設的
build.gradle
檔案,並將內容替換為以下程式碼:
apply plugin: 'java' apply plugin: 'application' mainClassName = 'CustomerQuickstart' sourceCompatibility = 1.7 targetCompatibility = 1.7 version = '1.0' repositories { mavenCentral() } dependencies { compile 'com.google.api-client:google-api-client:2.2.0' compile 'com.google.apis:google-api-services-androiddeviceprovisioning:v1-rev20230509-2.0.0' compile 'com.google.auth:google-auth-library-oauth2-http:1.16.1' compile 'com.google.auth:google-auth-library-credentials:1.16.1' compile 'com.google.http-client:google-http-client:1.43.1' compile 'com.google.oauth-client:google-oauth-client-jetty:1.34.1' }
步驟 3:設定範例
建立名為 src/main/java/CustomerQuickstart.java
的檔案,並複製下列程式碼,然後儲存檔案。
import com.google.api.client.googleapis.javanet.GoogleNetHttpTransport; import com.google.api.client.http.HttpRequestInitializer; import com.google.api.client.http.HttpTransport; import com.google.api.client.json.JsonFactory; import com.google.api.client.json.gson.GsonFactory; 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.CustomerListCustomersResponse; import com.google.api.services.androiddeviceprovisioning.v1.model.CustomerListDpcsResponse; import com.google.api.services.androiddeviceprovisioning.v1.model.Dpc; import com.google.auth.http.HttpCredentialsAdapter; import com.google.auth.oauth2.GoogleCredentials; import com.google.auth.oauth2.ServiceAccountCredentials; import java.io.IOException; import java.io.InputStream; 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 = Arrays.asList("https://www.googleapis.com/auth/androidworkzerotouchemm"); 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) { t.printStackTrace(); System.exit(1); } } /** * 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) .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(); // 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(); accountRequest.setPageSize(1); 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."); System.exit(-1); } 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 = service.customers().dpcs().list(customerAccount); 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()); } } }
步驟 4:執行範例
依照作業系統的說明,在檔案中執行指令碼。在 UNIX 和 Mac 電腦上,在終端機中執行下列指令:
gradle -q run
Notes
- 避免與他人共用
service_account_key.json
檔案。但請不要將它們加入原始碼存放區。詳情請參閱處理服務帳戶密鑰的建議做法。