सेवा खाते का इस्तेमाल करने वाले ग्राहकों के लिए, Java क्विकस्टार्ट
संग्रह की मदद से व्यवस्थित रहें
अपनी प्राथमिकताओं के आधार पर, कॉन्टेंट को सेव करें और कैटगरी में बांटें.
इस क्विकस्टार्ट गाइड में दिए गए निर्देशों का पालन करें और करीब 10 मिनट में
आसान Java कमांड-लाइन ऐप्लिकेशन, जो ज़ीरो-टच के लिए अनुरोध करता है
सेवा खाते का इस्तेमाल करके, एनरोलमेंट ग्राहक एपीआई का इस्तेमाल करें.
ज़रूरी शर्तें
यह क्विकस्टार्ट चलाने के लिए, आपके पास ये चीज़ें होनी चाहिए:
- कोई सेवा खाता, जो आपके पहले से तैयार डिवाइस के लिए ग्राहक खाते से जुड़ा हो. नीचे दिए गए लिंक पर क्लिक करने के बाद,
शुरू हुआ.
- Java 1.7 या इसके बाद का वर्शन.
- Gredle 2.3 या उसके बाद का वर्शन.
- इंटरनेट और वेब ब्राउज़र का ऐक्सेस.
पहला चरण: ज़ीरो-टच रजिस्ट्रेशन एपीआई को चालू करना
- Google Developers Console में कोई प्रोजेक्ट बनाने या चुनने के लिए, इस विज़र्ड का इस्तेमाल करें. इससे एपीआई अपने-आप चालू हो जाएगा. जारी रखें पर क्लिक करें. इसके बाद, क्रेडेंशियल पर जाएं
पर क्लिक करें.
- आपको कौनसा डेटा ऐक्सेस करना है? को ऐप्लिकेशन के डेटा पर सेट करें.
- आगे बढ़ें पर क्लिक करें. आपको एक सेवा बनाने के लिए कहा जाएगा
जोड़ें.
- सेवा खाते का नाम के लिए, जानकारी देने वाला नाम दें.
- सेवा खाते का आईडी (यह ईमेल पते की तरह दिखता है) नोट करें, क्योंकि इसका इस्तेमाल बाद में किया जाएगा.
- भूमिका को सेवा खाते > सेवा खाते के उपयोगकर्ता पर सेट करें.
- सेवा खाता बनाने के लिए, हो गया पर क्लिक करें.
- आपने जो सेवा खाता बनाया है उसके ईमेल पते पर क्लिक करें.
- **बटन** पर क्लिक करें.
- **कुंजी जोड़ें** पर क्लिक करें. इसके बाद, **नई कुंजी बनाएं** पर क्लिक करें.
- **कुंजी टाइप** के लिए, **JSON** चुनें.
- बनाएं पर क्लिक करें और अपने कंप्यूटर पर निजी कुंजी डाउनलोड करें.
- **बंद करें** पर क्लिक करें.
- फ़ाइल को अपनी वर्किंग डायरेक्ट्री में ले जाएं और उसका नाम बदलकर
service_account_key.json
कर दें.
दूसरा चरण: प्रोजेक्ट तैयार करना
अपना 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'
}
तीसरा चरण: सैंपल सेट अप करना
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());
}
}
}
चौथा चरण: सैंपल चलाना
फ़ाइल में स्क्रिप्ट चलाने के लिए, अपने ऑपरेटिंग सिस्टम की सहायता का इस्तेमाल करें. UNIX और Mac पर
कंप्यूटर को कॉपी करने के लिए, नीचे दिए गए कमांड को अपने टर्मिनल में चलाएं:
gradle -q run
नोट
- अपनी
service_account_key.json
फ़ाइल किसी के साथ शेयर न करें. सावधान रहें
उसे सोर्स कोड को स्टोर करने की जगहों में शामिल न करना चाहिए. सेवा खाते के पासवर्ड मैनेज करने के बारे में ज़्यादा सलाह पढ़ें.
ज़्यादा जानें
जब तक कुछ अलग से न बताया जाए, तब तक इस पेज की सामग्री को Creative Commons Attribution 4.0 License के तहत और कोड के नमूनों को Apache 2.0 License के तहत लाइसेंस मिला है. ज़्यादा जानकारी के लिए, Google Developers साइट नीतियां देखें. Oracle और/या इससे जुड़ी हुई कंपनियों का, Java एक रजिस्टर किया हुआ ट्रेडमार्क है.
आखिरी बार 2025-08-29 (UTC) को अपडेट किया गया.
[null,null,["आखिरी बार 2025-08-29 (UTC) को अपडेट किया गया."],[[["\u003cp\u003eThis quickstart guide helps you create a simple Java command-line application that interacts with the zero-touch enrollment customer API using a service account in approximately 10 minutes.\u003c/p\u003e\n"],["\u003cp\u003eYou will need a service account linked to your zero-touch enrollment customer account, Java 1.7 or greater, Gradle 2.3 or greater, and internet access to complete this process.\u003c/p\u003e\n"],["\u003cp\u003eThe guide involves enabling the zero-touch enrollment API, creating a service account, downloading a JSON key file, preparing a Gradle project, and setting up the Java sample code.\u003c/p\u003e\n"],["\u003cp\u003eThe created Java application will interact with the API by listing all the DPCs available using the customer account, showcasing the API's functionality.\u003c/p\u003e\n"],["\u003cp\u003eAfter completing the quickstart, there are multiple resources for further reading, including the Google Developers Console help documentation, Google APIs Client for Java documentation, and Google APIs Client Javadocs.\u003c/p\u003e\n"]]],["This guide outlines creating a Java command-line application to interact with the zero-touch enrollment customer API. First, enable the API and create a service account, noting its ID and downloading its JSON key. Next, set up a Gradle project, placing the service account key in the resources directory, and configuring the `build.gradle` file. Finally, create the `CustomerQuickstart.java` file, including code that gets account credentials, builds an API service, and requests and displays a list of available DPCs, then run using `gradle -q run`.\n"],null,["# Java quickstart for customers using a service account\n\nFollow the steps in this quickstart guide, and in about 10 minutes you have\na simple Java command-line app that makes requests to the zero-touch\nenrollment customer API using a service account.\n\nPrerequisites\n-------------\n\nTo run this quickstart, you need:\n\n- A service account, that's linked to you zero-touch enrollment customer account. See [Get\n started](/zero-touch/guides/customer/service-accounts).\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: 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` you downloaded when you created your\n service account into the `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```carbon\napply plugin: 'java'\napply plugin: 'application'\n\nmainClassName = 'CustomerQuickstart'\nsourceCompatibility = 1.7\ntargetCompatibility = 1.7\nversion = '1.0'\n\nrepositories {\n mavenCentral()\n}\n\ndependencies {\n compile 'com.google.api-client:google-api-client:2.2.0'\n compile 'com.google.apis:google-api-services-androiddeviceprovisioning:v1-rev20230509-2.0.0'\n compile 'com.google.auth:google-auth-library-oauth2-http:1.16.1'\n compile 'com.google.auth:google-auth-library-credentials:1.16.1'\n compile 'com.google.http-client:google-http-client:1.43.1'\n compile 'com.google.oauth-client:google-oauth-client-jetty:1.34.1'\n}\n```\n\nStep 3: Set up the sample\n-------------------------\n\nCreate a file named `src/main/java/CustomerQuickstart.java` and copy in the\nfollowing code and save the file. \n\n```java\nimport com.google.api.client.googleapis.javanet.GoogleNetHttpTransport;\nimport com.google.api.client.http.HttpRequestInitializer;\nimport com.google.api.client.http.HttpTransport;\nimport com.google.api.client.json.JsonFactory;\nimport com.google.api.client.json.gson.GsonFactory;\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.CustomerListCustomersResponse;\nimport com.google.api.services.androiddeviceprovisioning.v1.model.CustomerListDpcsResponse;\nimport com.google.api.services.androiddeviceprovisioning.v1.model.Dpc;\nimport com.google.auth.http.HttpCredentialsAdapter;\nimport com.google.auth.oauth2.GoogleCredentials;\nimport com.google.auth.oauth2.ServiceAccountCredentials;\nimport java.io.IOException;\nimport java.io.InputStream;\nimport java.util.Arrays;\nimport java.util.List;\n\n/** This class forms the quickstart introduction to the zero-touch enrollment customer API. */\npublic class CustomerQuickstart {\n\n // A single auth scope is used for the zero-touch enrollment customer API.\n private static final List\u003cString\u003e SCOPES =\n Arrays.asList(\"https://www.googleapis.com/auth/androidworkzerotouchemm\");\n private static final String APP_NAME = \"Zero-touch Enrollment Java Quickstart\";\n\n // Global shared instances\n private static final JsonFactory JSON_FACTORY = GsonFactory.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 GoogleCredentials object with the correct OAuth2 authorization for the service\n * account that calls the reseller API. The service endpoint invokes this method when setting up a\n * new service instance.\n *\n * @return an authorized GoogleCredentials object.\n * @throws IOException\n */\n public static GoogleCredentials authorize() throws IOException {\n // Load service account key.\n InputStream in = CustomerQuickstart.class.getResourceAsStream(\"/service_account_key.json\");\n\n // Create the credential scoped to the zero-touch enrollment customer APIs.\n GoogleCredentials credential = ServiceAccountCredentials.fromStream(in).createScoped(SCOPES);\n return credential;\n }\n\n /**\n * Build and return an authorized zero-touch enrollment API client service. Use the service\n * endpoint to call the API methods.\n *\n * @return an authorized client service endpoint\n * @throws IOException\n */\n public static AndroidProvisioningPartner getService() throws IOException {\n GoogleCredentials credential = authorize();\n HttpRequestInitializer requestInitializer = new HttpCredentialsAdapter(credential);\n return new AndroidProvisioningPartner.Builder(HTTP_TRANSPORT, JSON_FACTORY, requestInitializer)\n .setApplicationName(APP_NAME)\n .build();\n }\n\n /**\n * Runs the zero-touch enrollment quickstart app.\n *\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 // Get the customer's account. Because a customer might have more\n // than one, limit the results to the first account found.\n AndroidProvisioningPartner.Customers.List accountRequest = service.customers().list();\n accountRequest.setPageSize(1);\n CustomerListCustomersResponse accountResponse = accountRequest.execute();\n if (accountResponse.getCustomers().isEmpty()) {\n // No accounts found for the user. Confirm the Google Account\n // that authorizes the request can access the zero-touch portal.\n System.out.println(\"No zero-touch enrollment account found.\");\n System.exit(-1);\n }\n Company customer = accountResponse.getCustomers().get(0);\n String customerAccount = customer.getName();\n\n // Send an API request to list all the DPCs available using the customer account.\n AndroidProvisioningPartner.Customers.Dpcs.List request =\n service.customers().dpcs().list(customerAccount);\n CustomerListDpcsResponse response = request.execute();\n\n // Print out the details of each DPC.\n java.util.List\u003cDpc\u003e dpcs = response.getDpcs();\n for (Dpc dpcApp : dpcs) {\n System.out.format(\"Name:%s APK:%s\\n\", dpcApp.getDpcName(), dpcApp.getPackageName());\n }\n }\n}\n```\n\nStep 4: 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\nNotes\n-----\n\n- Avoid sharing your `service_account_key.json` file with anyone. Be careful not to include it in source code repositories. You can read more advice on [handling service account secrets](https://cloud.google.com/iam/docs/best-practices-for-managing-service-account-keys).\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)"]]