सेवा खाते का इस्तेमाल करने वाले ग्राहकों के लिए, Java क्विकस्टार्ट

इस क्विकस्टार्ट गाइड में दिए गए निर्देशों का पालन करें और करीब 10 मिनट में आपके पास एक आसान Java कमांड-लाइन ऐप्लिकेशन है. यह सेवा खाते की मदद से, ज़ीरो-टच नामांकन ग्राहक एपीआई के लिए अनुरोध करता है.

ज़रूरी शर्तें

इस क्विकस्टार्ट को चलाने के लिए, आपको इनकी ज़रूरत होगी:

पहला चरण: पहले से तैयार डिवाइस के लिए एपीआई चालू करना

  1. Google Developers Console में कोई प्रोजेक्ट बनाने या चुनने के लिए, इस विज़र्ड का इस्तेमाल करें और एपीआई को अपने-आप चालू करें. जारी रखें पर क्लिक करें. इसके बाद, क्रेडेंशियल पर जाएं .
  2. आपको कौनसा डेटा ऐक्सेस करना है? को ऐप्लिकेशन डेटा पर सेट करें.
  3. आगे बढ़ें पर क्लिक करें. आपको सेवा खाता बनाने के लिए कहा जाएगा.
  4. सेवा खाते के नाम की जानकारी देने वाला नाम दें.
  5. सेवा खाता आईडी देखें (यह एक ईमेल पते जैसा लगता है) क्योंकि आप बाद में इसका इस्तेमाल करेंगे.
  6. भूमिका को सेवा खाते > सेवा खाते के उपयोगकर्ता पर सेट करें.
  7. सेवा खाता बनाने की प्रोसेस पूरी करने के लिए, हो गया पर क्लिक करें.
  8. अपने बनाए गए सेवा खाते के ईमेल पते पर क्लिक करें.
  9. **बटन** पर क्लिक करें.
  10. **कुंजी जोड़ें** पर क्लिक करें. इसके बाद, **नई कुंजी बनाएं** पर क्लिक करें.
  11. **कुंजी टाइप** के लिए, **JSON** को चुनें.
  12. अपने कंप्यूटर पर, बनाएं और निजी कुंजी के डाउनलोड की जानकारी पर क्लिक करें.
  13. **बंद करें** पर क्लिक करें.
  14. फ़ाइल को अपनी काम करने की डायरेक्ट्री में ले जाएं और उसका नाम बदलें service_account_key.json.

दूसरा चरण: प्रोजेक्ट तैयार करना

अपना Gradle प्रोजेक्ट सेट अप करने के लिए, यह तरीका अपनाएं:

  1. चालू डायरेक्ट्री में नया प्रोजेक्ट बनाने के लिए, नीचे दिया गया कमांड रन करें:

    gradle init --type basic
    mkdir -p src/main/java src/main/resources
    
  2. ऊपर दी गई src/main/resources/ डायरेक्ट्री में अपना सेवा खाता बनाते समय डाउनलोड किए गए service_account_key.json को कॉपी करें.

  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.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 फ़ाइल किसी के साथ शेयर करने से बचें. ध्यान रखें कि इसे सोर्स कोड के डेटा स्टोर करने की जगहों में शामिल न किया जाए. ज़्यादा जानकारी के लिए, सेवा खाते के सीक्रेट मैनेज करने से जुड़ी सलाह पढ़ें.

ज़्यादा जानें