ทําตามขั้นตอนในคู่มือเริ่มใช้งานฉบับย่อนี้ และในอีก 10 นาทีคุณก็จะมีแอปบรรทัดคําสั่ง Java ที่ส่งคําขอไปยัง API ลูกค้าการตั้งค่าอุปกรณ์พร้อมใช้แบบรวมกลุ่ม
สิ่งที่ต้องดำเนินการก่อน
หากต้องการเรียกใช้การเริ่มต้นอย่างรวดเร็วนี้ คุณต้องมีสิ่งต่อไปนี้
- บัญชี Google ที่เป็นสมาชิกของบัญชีลูกค้า การตั้งค่าอุปกรณ์พร้อมใช้แบบรวมกลุ่ม โปรดดูเริ่มต้นใช้งาน
- Java 1.7 หรือสูงกว่า
- เวอร์ชัน 2.3 ขึ้นไป
- เข้าถึงอินเทอร์เน็ตและเว็บเบราว์เซอร์
ขั้นตอนที่ 1: เปิด API การตั้งค่าอุปกรณ์พร้อมใช้แบบรวมกลุ่ม
- ใช้วิซาร์ดนี้เพื่อสร้างหรือเลือกโปรเจ็กต์ใน Google Developers Console และเปิด API โดยอัตโนมัติ คลิกต่อไป แล้วคลิกไปที่ข้อมูลรับรอง
- คลิกยกเลิกที่ข้อมูลรับรองการสร้าง
- เลือกแท็บหน้าจอคํายินยอม OAuth ที่ด้านบนของหน้า เลือกที่อยู่อีเมล ป้อนชื่อผลิตภัณฑ์หากยังไม่ได้ตั้งค่าไว้ แล้วคลิกปุ่มบันทึก
- เลือกแท็บข้อมูลเข้าสู่ระบบ คลิกปุ่มสร้างข้อมูลเข้าสู่ระบบ และเลือกรหัสไคลเอ็นต์ OAuth
- เลือกประเภทแอปพลิเคชันเป็นอื่นๆ จากนั้นป้อนชื่อ "Quickstart" และคลิกปุ่มสร้าง
- คลิกตกลงเพื่อปิดแผงไคลเอ็นต์ OAuth
- คลิก ดาวน์โหลด JSON
- ย้ายไฟล์ไปยังไดเรกทอรีการทํางานและเปลี่ยนชื่อ
client_secret.json
ขั้นตอนที่ 2: จัดเตรียมโครงการ
ทําตามขั้นตอนต่อไปนี้เพื่อตั้งค่าโปรเจ็กต์ Gradle
เรียกใช้คําสั่งต่อไปนี้เพื่อสร้างโปรเจ็กต์ใหม่ในไดเรกทอรีที่ใช้งานได้
gradle init --type basic mkdir -p src/main/java src/main/resources
คัดลอกไฟล์
client_secret.json
ที่ดาวน์โหลดไว้ในขั้นตอนที่ 1 ไปยังไดเรกทอรี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.oauth-client:google-oauth-client-jetty:1.34.1' }
ขั้นตอนที่ 3: ตั้งค่าตัวอย่าง
สร้างไฟล์ชื่อ src/main/java/CustomerQuickstart.java
และคัดลอกโค้ดต่อไปนี้ และบันทึกไฟล์
import com.google.api.client.auth.oauth2.Credential; import com.google.api.client.extensions.java6.auth.oauth2.AuthorizationCodeInstalledApp; import com.google.api.client.extensions.jetty.auth.oauth2.LocalServerReceiver; import com.google.api.client.googleapis.auth.oauth2.GoogleAuthorizationCodeFlow; import com.google.api.client.googleapis.auth.oauth2.GoogleClientSecrets; 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.gson.GsonFactory; import com.google.api.client.util.store.FileDataStoreFactory; 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 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 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"; private static final java.io.File DATA_STORE_DIR = new java.io.File(System.getProperty("user.home"), ".credentials/zero-touch.quickstart.json"); // Global shared instances private static FileDataStoreFactory DATA_STORE_FACTORY; private static final JsonFactory JSON_FACTORY = GsonFactory.getDefaultInstance(); private static HttpTransport HTTP_TRANSPORT; static { try { HTTP_TRANSPORT = GoogleNetHttpTransport.newTrustedTransport(); DATA_STORE_FACTORY = new FileDataStoreFactory(DATA_STORE_DIR); } catch (Throwable t) { t.printStackTrace(); System.exit(1); } } /** * Creates a Credential object with the correct OAuth2 authorization for the user calling the * customer 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 client secrets. InputStream in = CustomerQuickstart.class.getResourceAsStream("/client_secret.json"); GoogleClientSecrets clientSecrets = GoogleClientSecrets.load(JSON_FACTORY, new InputStreamReader(in, "UTF-8")); // Ask the user to authorize the request using their Google Account // in their browser. GoogleAuthorizationCodeFlow flow = new GoogleAuthorizationCodeFlow.Builder(HTTP_TRANSPORT, JSON_FACTORY, clientSecrets, SCOPES) .setDataStoreFactory(DATA_STORE_FACTORY) .setAccessType("offline") .build(); Credential credential = new AuthorizationCodeInstalledApp(flow, new LocalServerReceiver()).authorize("user"); System.out.println("Credential file saved to: " + DATA_STORE_DIR.getAbsolutePath()); 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 { 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(); // 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
คุณจะต้องให้สิทธิ์ในการเข้าถึงเมื่อเรียกใช้แอปเป็นครั้งแรก ดังนี้
- แอปพลิเคชันจะพยายามเปิดแท็บใหม่ในเบราว์เซอร์เริ่มต้นของคุณ หากไม่สําเร็จ ให้คัดลอก URL จากคอนโซลและเปิดในเบราว์เซอร์ หากคุณยังไม่ได้เข้าสู่ระบบบัญชี Google ระบบจะขอให้คุณลงชื่อเข้าสู่ระบบ หากคุณเข้าสู่ระบบบัญชี Google หลายบัญชี หน้านี้จะแสดงขึ้นมาให้คุณเลือกบัญชีที่จะให้สิทธิ์
- คลิกยอมรับ
- ปิดแท็บเบราว์เซอร์ แอปจะยังคงทํางานต่อไป
หมายเหตุ
- เนื่องจากไลบรารีของไคลเอ็นต์ Google API จัดเก็บข้อมูลการให้สิทธิ์ไว้ในระบบไฟล์ การเปิดใช้งานครั้งต่อๆ ไปจึงไม่แจ้งให้คุณให้สิทธิ์
- หากต้องการรีเซ็ตข้อมูลการให้สิทธิ์ของแอป ให้ลบไฟล์
~/.credentials/zero-touch.quickstart.json
และเรียกใช้แอปอีกครั้ง - ขั้นตอนการให้สิทธิ์ในการเริ่มต้นอย่างรวดเร็วนี้เหมาะสําหรับแอปบรรทัดคําสั่ง หากต้องการดูวิธีเพิ่มการให้สิทธิ์ไปยังเว็บแอป ให้ดูที่ แอปพลิเคชันเว็บเซิร์ฟเวอร์ OAuth 2.0
การแก้ปัญหา
คุณจะต้องตรวจสอบสิ่งทั่วไปต่อไปนี้ โปรดแจ้งให้เราทราบว่ามีอะไรผิดปกติจากการเริ่มต้นอย่างรวดเร็ว เพื่อให้เราแก้ไขปัญหาดังกล่าว
- โปรดตรวจสอบว่าคุณให้สิทธิ์การเรียก API ด้วยบัญชี Google เดียวกับที่เป็นสมาชิกของบัญชีลูกค้า การตั้งค่าอุปกรณ์พร้อมใช้แบบรวมกลุ่ม ลองลงชื่อเข้าใช้พอร์ทัลการตั้งค่าอุปกรณ์พร้อมใช้แบบรวมกลุ่มโดยใช้บัญชี Google เดียวกันเพื่อทดสอบการเข้าถึง
- ตรวจสอบว่าบัญชียอมรับข้อกําหนดในการให้บริการล่าสุดในพอร์ทัลแล้ว โปรดดู บัญชีลูกค้า