การเริ่มต้นใช้งาน Java อย่างรวดเร็วสำหรับลูกค้า

ทำตามขั้นตอนในคู่มือเริ่มใช้งานฉบับย่อนี้ และในอีกประมาณ 10 นาทีคุณก็จะได้ แอปบรรทัดคำสั่ง Java ง่ายๆ ที่ส่งคำขอไปยังการตั้งค่าอุปกรณ์พร้อมใช้แบบรวมกลุ่ม การลงทะเบียน API ลูกค้า

ข้อกำหนดเบื้องต้น

หากต้องการเรียกใช้การเริ่มต้นอย่างรวดเร็วนี้ คุณต้องมีสิ่งต่อไปนี้

  • บัญชี Google ซึ่งเป็นสมาชิกลูกค้าการตั้งค่าอุปกรณ์พร้อมใช้แบบรวมกลุ่ม ของคุณได้ ดูที่รับ แล้ว
  • Java 1.7 หรือสูงกว่า
  • Gradle 2.3 ขึ้นไป
  • เข้าถึงอินเทอร์เน็ตและเว็บเบราว์เซอร์

ขั้นตอนที่ 1: เปิด API การตั้งค่าอุปกรณ์พร้อมใช้แบบรวมกลุ่ม

  1. ใช้ this วิซาร์ดเพื่อสร้างหรือเลือกโปรเจ็กต์ใน Google Developers Console และ เปิด API โดยอัตโนมัติ คลิกต่อไป แล้วคลิกไปที่ข้อมูลเข้าสู่ระบบ
  2. คลิกยกเลิกในส่วนสร้างข้อมูลเข้าสู่ระบบ
  3. เลือกแท็บหน้าจอขอความยินยอม OAuth ที่ด้านบนของหน้า เลือก อีเมล ป้อนชื่อผลิตภัณฑ์หากยังไม่ได้ตั้งค่า และ คลิกปุ่มบันทึก
  4. เลือกแท็บข้อมูลเข้าสู่ระบบ แล้วคลิกสร้างข้อมูลเข้าสู่ระบบ แล้วเลือกรหัสไคลเอ็นต์ OAuth
  5. เลือกประเภทแอปพลิเคชัน Other จากนั้นป้อนชื่อ "การเริ่มต้นอย่างรวดเร็ว" และคลิกสร้าง
  6. คลิกตกลงเพื่อปิดแผงไคลเอ็นต์ OAuth
  7. คลิก ดาวน์โหลด JSON
  8. ย้ายไฟล์ไปยังไดเรกทอรีที่ใช้งานอยู่และเปลี่ยนชื่อ client_secret.json

ขั้นตอนที่ 2: เตรียมโครงการ

โปรดทำตามขั้นตอนด้านล่างเพื่อตั้งค่าโปรเจ็กต์ Gradle

  1. เรียกใช้คำสั่งต่อไปนี้เพื่อสร้างโปรเจ็กต์ใหม่ในไดเรกทอรีการทำงาน

    gradle init --type basic
    mkdir -p src/main/java src/main/resources
    
  2. คัดลอกไฟล์ client_secret.json ที่คุณดาวน์โหลดในขั้นตอนที่ 1 ไปยัง src/main/resources/ไดเรกทอรีที่คุณสร้างไว้ข้างต้น

  3. เปิดไฟล์ 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

ครั้งแรกที่เรียกใช้แอป คุณจะต้องให้สิทธิ์เข้าถึงดังนี้

  1. แอปจะพยายามเปิดแท็บใหม่ในเบราว์เซอร์เริ่มต้นของคุณ หากไม่สำเร็จ ให้คัดลอก URL จาก คอนโซลและเปิดในเบราว์เซอร์ หากคุณยังไม่ได้ลงชื่อเข้าใช้บัญชี Google ของคุณ แสดงว่า แจ้งให้เข้าสู่ระบบ หากคุณเข้าสู่ระบบบัญชี Google หลายบัญชี หน้าเว็บจะแจ้งให้เลือก บัญชีสำหรับการให้สิทธิ์
  2. คลิกยอมรับ
  3. ปิดแท็บเบราว์เซอร์ - แอปจะยังทำงานต่อไป

หมายเหตุ

  • เนื่องจากไลบรารีของไคลเอ็นต์ Google API จัดเก็บข้อมูลการให้สิทธิ์ไว้ในระบบไฟล์ ดังนั้น ระบบจะไม่แสดงคำขอการให้สิทธิ์
  • หากต้องการรีเซ็ตข้อมูลการให้สิทธิ์ของแอป ให้ลบ ~/.credentials/zero-touch.quickstart.json ไฟล์และเรียกใช้แอปอีกครั้ง
  • ขั้นตอนการให้สิทธิ์ในการเริ่มต้นอย่างรวดเร็วนี้เหมาะสำหรับแอปบรรทัดคำสั่ง ดูวิธีเพิ่ม การให้สิทธิ์สำหรับเว็บแอป ให้ดูที่ แอปพลิเคชันเว็บเซิร์ฟเวอร์ OAuth 2.0

การแก้ปัญหา

ต่อไปนี้เป็นข้อมูลทั่วไปที่คุณควรตรวจสอบ แจ้งให้เราทราบถึงข้อผิดพลาดของการเริ่มต้นอย่างรวดเร็ว แล้วเราจะดำเนินการแก้ไข

  • ตรวจสอบว่าคุณให้สิทธิ์การเรียก API ด้วยบัญชี Google เดียวกับที่เป็นสมาชิกของ บัญชีลูกค้าการตั้งค่าอุปกรณ์พร้อมใช้แบบรวมกลุ่ม ลองลงชื่อเข้าใช้พอร์ทัลการตั้งค่าอุปกรณ์พร้อมใช้แบบรวมกลุ่มโดยใช้ บัญชี Google เดียวกันเพื่อทดสอบการเข้าถึง
  • ยืนยันว่าบัญชียอมรับข้อกำหนดในการให้บริการล่าสุดใน พอร์ทัล โปรดดู บัญชีลูกค้า

ดูข้อมูลเพิ่มเติม