Java Quickstart สำหรับตัวแทนจำหน่าย
จัดทุกอย่างให้เป็นระเบียบอยู่เสมอด้วยคอลเล็กชัน
บันทึกและจัดหมวดหมู่เนื้อหาตามค่ากำหนดของคุณ
ทำตามขั้นตอนในคู่มือเริ่มต้นใช้งานฉบับย่อนี้ และคุณจะมีแอปบรรทัดคำสั่ง Java ง่ายๆ ที่ส่งคำขอไปยัง Reseller API สำหรับการลงทะเบียนแบบรวมศูนย์ภายในเวลาประมาณ 10 นาที
ข้อกำหนดเบื้องต้น
หากต้องการเรียกใช้การเริ่มต้นใช้งานอย่างรวดเร็วนี้ คุณต้องมีสิ่งต่อไปนี้
- บัญชี Google ซึ่งเป็นสมาชิกบัญชีตัวแทนจำหน่ายการตั้งค่าอุปกรณ์พร้อมใช้แบบรวมกลุ่ม หากยังไม่ได้เริ่มต้นใช้งาน ให้ทำตามขั้นตอนในเริ่มต้นใช้งานในคู่มือพอร์ทัลตัวแทนจำหน่าย
- Java 1.7 ขึ้นไป
- Gradle 2.3 ขึ้นไป
- เข้าถึงอินเทอร์เน็ตและเว็บเบราว์เซอร์
ขั้นตอนที่ 1: เปิด API การตั้งค่าอุปกรณ์พร้อมใช้แบบรวมกลุ่ม
- ใช้วิซาร์ดนี้เพื่อสร้างหรือเลือกโปรเจ็กต์ใน Google Developers Console และเปิด API โดยอัตโนมัติ คลิกต่อไป แล้วคลิกไปที่ข้อมูลเข้าสู่ระบบ
- ตั้งค่าคุณต้องการเข้าถึงข้อมูลใดเป็นข้อมูลแอปพลิเคชัน
- คลิกถัดไป ระบบจะแจ้งให้คุณสร้างบัญชีบริการ
- ตั้งชื่อที่สื่อความหมายสำหรับชื่อบัญชีบริการ
- จดบันทึกรหัสบัญชีบริการ (มีลักษณะคล้ายอีเมล) เนื่องจากคุณจะต้องใช้รหัสนี้ในภายหลัง
- ตั้งค่าบทบาทเป็นบัญชีบริการ > ผู้ใช้บัญชีบริการ
- คลิกเสร็จสิ้นเพื่อสร้างบัญชีบริการให้เสร็จสมบูรณ์
- คลิกอีเมลของบัญชีบริการที่คุณสร้างขึ้น
- คลิก**กุญแจ**
- คลิก**เพิ่มคีย์** แล้วคลิก**สร้างคีย์ใหม่**
- เลือก **JSON** สำหรับ**ประเภทคีย์**
- คลิกสร้าง แล้วดาวน์โหลดคีย์ส่วนตัวลงในคอมพิวเตอร์
- คลิก **ปิด**
- ย้ายไฟล์ไปยังไดเรกทอรีทํางาน แล้วเปลี่ยนชื่อเป็น
service_account_key.json
ขั้นตอนที่ 2: ลิงก์บัญชีบริการ
- เปิดพอร์ทัลการตั้งค่าอุปกรณ์พร้อมใช้แบบรวมกลุ่ม คุณอาจต้องลงชื่อเข้าใช้
- คลิก settings_ethernet บัญชีบริการ
- คลิกadd ลิงก์บัญชีบริการ
- ตั้งค่าอีเมลเป็นอีเมลของบัญชีบริการที่คุณสร้างขึ้น
- คลิกลิงก์บัญชีบริการเพื่อใช้บัญชีบริการกับบัญชีลงทะเบียนการตั้งค่าอุปกรณ์พร้อมใช้
ขั้นตอนที่ 3: เตรียมโปรเจ็กต์
ทำตามขั้นตอนด้านล่างเพื่อตั้งค่าโปรเจ็กต์ Gradle
เรียกใช้คำสั่งต่อไปนี้เพื่อสร้างโปรเจ็กต์ใหม่ในไดเรกทอรีที่ทำงานอยู่
gradle init --type basic
mkdir -p src/main/java src/main/resources
คัดลอกไฟล์ service_account_key.json
ที่ดาวน์โหลดไว้ในขั้นตอนที่ 1 ไปยังไดเรกทอรี src/main/resources/
ที่คุณสร้างขึ้นด้านบน
เปิดไฟล์ 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
แล้วคัดลอกโค้ดต่อไปนี้ลงในไฟล์ แล้วบันทึกไฟล์ แทรก reseller partner
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");
}
}
}
รหัสพาร์ทเนอร์
โดยปกติการเรียก API ต้องใช้รหัสพาร์ทเนอร์ตัวแทนจำหน่ายของคุณเป็นอาร์กิวเมนต์ หากต้องการดูรหัสพาร์ทเนอร์จากพอร์ทัลการตั้งค่าอุปกรณ์พร้อมใช้แบบรวมกลุ่ม ให้ทำตามขั้นตอนต่อไปนี้
- เปิดพอร์ทัล คุณอาจต้องลงชื่อเข้าใช้
- คลิก settings_ethernet บัญชีบริการ
- คัดลอกหมายเลขรหัสพาร์ทเนอร์จากบรรทัดรหัสตัวแทนจำหน่ายของคุณ
ขั้นตอนที่ 5: เรียกใช้ตัวอย่าง
ใช้ความช่วยเหลือของระบบปฏิบัติการเพื่อเรียกใช้สคริปต์ในไฟล์ ในคอมพิวเตอร์ที่ใช้ UNIX และ Mac ให้เรียกใช้คำสั่งด้านล่างในเทอร์มินัล
gradle -q run
การแก้ปัญหา
บอกเราเกี่ยวกับปัญหาที่เกิดขึ้นกับวิธีเริ่มต้นใช้งานด่วน แล้วเราจะพยายามแก้ไข หากต้องการดูวิธีที่การตั้งค่าอุปกรณ์พร้อมใช้แบบรวมกลุ่มใช้บัญชีบริการเพื่อให้สิทธิ์การเรียก API โปรดอ่านการให้สิทธิ์
ดูข้อมูลเพิ่มเติม
เนื้อหาของหน้าเว็บนี้ได้รับอนุญาตภายใต้ใบอนุญาตที่ต้องระบุที่มาของครีเอทีฟคอมมอนส์ 4.0 และตัวอย่างโค้ดได้รับอนุญาตภายใต้ใบอนุญาต Apache 2.0 เว้นแต่จะระบุไว้เป็นอย่างอื่น โปรดดูรายละเอียดที่นโยบายเว็บไซต์ Google Developers Java เป็นเครื่องหมายการค้าจดทะเบียนของ Oracle และ/หรือบริษัทในเครือ
อัปเดตล่าสุด 2025-08-29 UTC
[null,null,["อัปเดตล่าสุด 2025-08-29 UTC"],[[["\u003cp\u003eThis guide walks you through setting up a simple Java command-line application that interacts with the zero-touch enrollment reseller API in approximately 10 minutes.\u003c/p\u003e\n"],["\u003cp\u003eTo begin, you'll need a Google account within your zero-touch enrollment reseller account, Java 1.7 or later, Gradle 2.3 or later, and internet access.\u003c/p\u003e\n"],["\u003cp\u003eThe process involves turning on the zero-touch enrollment API, creating and linking a service account within the portal, and downloading a JSON key for authentication.\u003c/p\u003e\n"],["\u003cp\u003eYou'll then prepare a Gradle project, copy the service account key, set up the sample Java code with your reseller partner ID, and finally run the application using a simple command.\u003c/p\u003e\n"],["\u003cp\u003eThe code when run will list all customers associated with your reseller partner id.\u003c/p\u003e\n"]]],["First, enable the zero-touch enrollment API and create a service account, noting its ID and downloading its JSON key. Link this service account in the zero-touch portal. Next, set up a Gradle project, placing the downloaded key in the `src/main/resources/` directory and modify the `build.gradle` file. Then create `ResellerQuickstart.java`, replacing the placeholder `PARTNER_ID` with your actual reseller partner ID. Finally, execute the project using the `gradle -q run` command.\n"],null,["# Java quickstart for resellers\n\nFollow the steps in this quickstart guide, and in about 10 minutes you'll have\na simple Java command-line app that makes requests to the zero-touch\nenrollment reseller API.\n\nPrerequisites\n-------------\n\nTo run this quickstart, you'll need:\n\n- A Google account, that's a member of your zero-touch enrollment reseller account. If you haven't onboarded yet, follow the steps in *Get started* in the [Reseller portal guide](/zero-touch/guides/portal).\n- Java 1.7 or greater.\n- [Gradle 2.3 or greater](http://gradle.org/downloads).\n- Access to the internet and a web browser.\n\nStep 1: Turn on the zero-touch enrollment API\n---------------------------------------------\n\n1. Use [this\n wizard](https://console.developers.google.com/start/api?id=androiddeviceprovisioning.googleapis.com) to create or select a project in the Google Developers Console and automatically turn on the API. Click **Continue** , then **Go to credentials**.\n2. Set **What data will you be accessing?** to *Application data*.\n3. Click **Next**. You should be prompted to create a service account.\n4. Give a descriptive name for **Service account name**.\n5. Note the **Service account ID** (it looks like an email address) because you'll use it later.\n6. Set **Role** to *Service Accounts \\\u003e Service Account User*.\n7. Click **Done** to finish creating the service account.\n8. Click the email address for the service account that you created.\n9. Click \\*\\*Keys\\*\\*.\n10. Click \\*\\*Add key\\*\\*, then click \\*\\*Create new key\\*\\*.\n11. For \\*\\*Key type\\*\\*, select \\*\\*JSON\\*\\*.\n12. Click **Create** and the private key downloads to your computer.\n13. Click \\*\\*Close\\*\\*.\n14. Move the file to your working directory and rename it `service_account_key.json`.\n\n| **Warning:** Service account keys can become a security risk if not managed carefully. For advice see [best practices for managing API keys](https://cloud.google.com/iam/docs/best-practices-for-managing-service-account-keys).\n\nStep 2: Link the service account\n--------------------------------\n\n1. Open the zero-touch enrollment [portal](https://enterprise.google.com/android/zero-touch/resellers). You might need to sign in.\n2. Click settings_ethernet **Service\n accounts**.\n3. Click add **Link service account**.\n4. Set **Email address** to the address of the service account you created.\n5. Click **Link service account** to use the service account with your zero-touch enrollment account.\n\nStep 3: Prepare the project\n---------------------------\n\nFollow the steps below to set up your Gradle project:\n\n1. Run the following command to create a new project in the working directory:\n\n gradle init --type basic\n mkdir -p src/main/java src/main/resources\n\n2. Copy the `service_account_key.json` file you downloaded in Step 1 into the\n `src/main/resources/` directory you created above.\n\n3. Open the default `build.gradle` file and replace its contents with the\n following code:\n\n apply plugin: 'java'\n apply plugin: 'application'\n\n mainClassName = 'ResellerQuickstart'\n sourceCompatibility = 1.7\n targetCompatibility = 1.7\n version = '1.0'\n\n repositories {\n mavenCentral()\n }\n\n dependencies {\n compile 'com.google.api-client:google-api-client:1.30.11'\n compile 'com.google.apis:google-api-services-androiddeviceprovisioning:+'\n compile 'com.google.oauth-client:google-oauth-client-jetty:+'\n }\n\nStep 4: Set up the sample\n-------------------------\n\nCreate a file named `src/main/java/ResellerQuickstart.java` and copy in the\nfollowing code and save the file. Insert your own [reseller partner\nID](#partner-id) as the value for `PARTNER_ID` (the app's first line). \n\n```java\nimport com.google.api.client.auth.oauth2.Credential;\nimport com.google.api.client.googleapis.auth.oauth2.GoogleCredential;\nimport com.google.api.client.googleapis.javanet.GoogleNetHttpTransport;\nimport com.google.api.client.http.HttpTransport;\nimport com.google.api.client.json.JsonFactory;\nimport com.google.api.client.json.jackson2.JacksonFactory;\nimport com.google.api.services.androiddeviceprovisioning.v1.AndroidProvisioningPartner;\nimport com.google.api.services.androiddeviceprovisioning.v1.model.Company;\nimport com.google.api.services.androiddeviceprovisioning.v1.model.ListCustomersResponse;\nimport java.io.IOException;\nimport java.io.InputStream;\nimport java.io.InputStreamReader;\nimport java.util.Arrays;\nimport java.util.List;\n\n/**\n * This class forms the quickstart introduction to the zero-touch enrollemnt\n * reseller API.\n */\npublic class ResellerQuickstart {\n\n // TODO: replace this with your partner reseller ID.\n private static long PARTNER_ID = 11036885;\n\n // Use a single scope for the all methods in the reseller API.\n private static final List\u003cString\u003e SCOPES =\n Arrays.asList(\"https://www.googleapis.com/auth/androidworkprovisioning\");\n private static final String APP_NAME = \"Zero-touch Reseller Java Quickstart\";\n\n // Global shared instances.\n private static final JsonFactory JSON_FACTORY = JacksonFactory.getDefaultInstance();\n private static HttpTransport HTTP_TRANSPORT;\n\n static {\n try {\n HTTP_TRANSPORT = GoogleNetHttpTransport.newTrustedTransport();\n } catch (Throwable t) {\n t.printStackTrace();\n System.exit(1);\n }\n }\n\n /**\n * Creates a Credential object with the correct OAuth2 authorization\n * for the service account that calls the reseller API. The service\n * endpoint invokes this method when setting up a new service instance.\n * @return an authorized Credential object.\n * @throws IOException\n */\n public static Credential authorize() throws IOException {\n // Load the service account key from the JSON file.\n InputStream in =\n ResellerQuickstart.class.getResourceAsStream(\"/service_account_key.json\");\n\n // Create the credential scoped to the zero-touch enrollemnt\n // reseller APIs.\n GoogleCredential credential = GoogleCredential\n .fromStream(in)\n .createScoped(SCOPES);\n return credential;\n }\n\n /**\n * Builds and returns an authorized zero-touch enrollment API client service.\n * Use the service endpoint to call the API methods.\n * @return an authorized client service endpoint\n * @throws IOException\n */\n public static AndroidProvisioningPartner getService() throws IOException {\n Credential credential = authorize();\n return new AndroidProvisioningPartner.Builder(\n HTTP_TRANSPORT, JSON_FACTORY, credential)\n .setApplicationName(APP_NAME)\n .build();\n }\n\n /**\n * Runs the zero-touch enrollment quickstart app.\n * @throws IOException\n */\n public static void main(String[] args) throws IOException {\n\n // Create a zero-touch enrollment API service endpoint.\n AndroidProvisioningPartner service = getService();\n\n // Send an API request to list all our customers.\n AndroidProvisioningPartner.Partners.Customers.List request =\n service.partners().customers().list(PARTNER_ID);\n ListCustomersResponse response = request.execute();\n\n // Print out the details of each customer.\n if (response.getCustomers() != null) {\n java.util.List\u003cCompany\u003e customers = response.getCustomers();\n for (Company customer : customers) {\n System.out.format(\"Name:%s ID:%d\\n\",\n customer.getCompanyName(),\n customer.getCompanyId());\n }\n } else {\n System.out.println(\"No customers found\");\n }\n }\n}\n```\n\n### Partner ID\n\nAPI calls typically need your reseller partner ID as an argument. To find your\npartner ID from the zero-touch enrollment portal, follow the steps below:\n\n1. Open the [portal](https://enterprise.google.com/android/zero-touch/resellers). You might need to sign in.\n2. Click settings_ethernet **Service\n accounts**.\n3. Copy your partner ID number from the **Your reseller ID** line.\n\nStep 5: Run the sample\n----------------------\n\nUse your operating system's help to run the script in the file. On UNIX and Mac\ncomputers, run the command below in your terminal: \n\n gradle -q run\n\nTroubleshooting\n---------------\n\n\n[Tell us what went wrong]() with the quickstart and we'll work to\nfix it. To learn how zero-touch uses service accounts to authorize API calls, read\n[Authorization](/zero-touch/guides/auth).\n\nLearn more\n----------\n\n- [Google Developers Console help documentation](/console/help/new)\n- [Google APIs Client for Java documentation](/api-client-library/java)\n- [Google APIs Client Javadocs](/api-client-library/java/google-api-java-client/reference)"]]