ทําตามขั้นตอนในคู่มือเริ่มใช้งานฉบับย่อนี้ และในอีก 10 นาทีคุณก็จะมีแอปบรรทัดคําสั่ง Java ที่ส่งคําขอไปยัง API ลูกค้าการตั้งค่าอุปกรณ์พร้อมใช้แบบรวมกลุ่มโดยใช้บัญชีบริการ
สิ่งที่ต้องดำเนินการก่อน
หากต้องการเรียกใช้การเริ่มต้นอย่างรวดเร็วนี้ คุณต้องมีสิ่งต่อไปนี้
- บัญชีบริการที่ลิงก์กับบัญชีลูกค้าการตั้งค่าอุปกรณ์พร้อมใช้แบบรวมกลุ่มของคุณ โปรดดูเริ่มต้นใช้งาน
- Java 1.7 หรือสูงกว่า
- เวอร์ชัน 2.3 ขึ้นไป
- เข้าถึงอินเทอร์เน็ตและเว็บเบราว์เซอร์
ขั้นตอนที่ 1: เปิด API การตั้งค่าอุปกรณ์พร้อมใช้แบบรวมกลุ่ม
- ใช้วิซาร์ดนี้เพื่อสร้างหรือเลือกโปรเจ็กต์ใน Google Developers Console และเปิด API โดยอัตโนมัติ คลิกต่อไป แล้วคลิกไปที่ข้อมูลรับรอง
- กําหนดคุณจะเข้าถึงข้อมูลใดบ้างเป็นข้อมูลแอปพลิเคชัน
- คลิกถัดไป ระบบควรแจ้งให้สร้างบัญชีบริการ
- ตั้งชื่อที่สื่อความหมายสําหรับชื่อบัญชีบริการ
- จดรหัสบัญชีบริการ (ลักษณะเหมือนที่อยู่อีเมล) เพราะคุณจะต้องใช้รหัสในภายหลัง
- ตั้งค่าบทบาทเป็นบัญชีบริการ > ผู้ใช้บัญชีบริการ
- คลิกเสร็จสิ้นเพื่อจบการสร้างบัญชี
- คลิกที่อยู่อีเมลของบัญชีบริการที่คุณสร้างขึ้น
- คลิก **คีย์**
- คลิก **เพิ่มคีย์** จากนั้นคลิก **สร้างคีย์ใหม่**
- สําหรับ **ประเภทคีย์** ให้เลือก **JSON**
- คลิกสร้าง และการดาวน์โหลดคีย์ส่วนตัวลงในคอมพิวเตอร์
- คลิก **ปิด**
- ย้ายไฟล์ไปยังไดเรกทอรีการทํางานและเปลี่ยนชื่อ
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
หมายเหตุ
- หลีกเลี่ยงการแชร์ไฟล์
service_account_key.json
กับคนอื่น โปรดระวัง อย่ารวมรหัสนี้ไว้ในที่เก็บซอร์สโค้ด คุณอ่านคําแนะนําเพิ่มเติมเกี่ยวกับการจัดการข้อมูลลับของบัญชีบริการได้