Wykonaj czynności opisane w tym krótkim przewodniku, a w ciągu około 10 minut masz do dyspozycji prostą aplikację wiersza poleceń w języku Java, która wysyła żądania do interfejsu API rejestracji typu zero-touch za pomocą konta usługi.
Wymagania wstępne
Aby to zrobić, potrzebujesz:
- Konto usługi połączone z kontem klienta rejestracji typu zero-touch. Zobacz Pierwsze kroki.
- Java 1.7 lub nowsza.
- Gradle w wersji 2.3 lub nowszej.
- Dostęp do internetu i przeglądarki.
Krok 1. Włącz interfejs API do rejestracji typu zero-touch
- Aby utworzyć lub wybrać projekt w Google Developers Console, i automatycznie włączyć ten interfejs API, użyj tego kreatora. Kliknij Dalej, a następnie Otwórz dane logowania.
- Ustaw Jakie dane będą dostępne? na Dane aplikacji.
- Kliknij Dalej. Pojawi się prośba o utworzenie konta usługi.
- Nadaj nazwę konta usługi opisową.
- Zapisz identyfikator konta usługi (wygląda jak adres e-mail), ponieważ będziesz go używać później.
- Ustaw Rolę na Konta usługi > Użytkownik konta usługi.
- Aby zakończyć tworzenie konta usługi, kliknij Gotowe.
- Kliknij adres e-mail utworzonego przez siebie konta usługi.
- Kliknij **Klucze**.
- Kliknij **Dodaj klucz**, a następnie **Utwórz nowy klucz**.
- W polu **Typ klucza** wybierz **JSON**.
- Kliknij Utwórz, aby pobrać klucz prywatny na komputer.
- Kliknij **Zamknij**.
- Przenieś plik do katalogu roboczego i zmień jego nazwę na
service_account_key.json
.
Krok 2. Przygotuj projekt
Aby skonfigurować projekt Gradle, wykonaj te czynności:
Aby utworzyć nowy projekt w katalogu roboczym, uruchom to polecenie:
gradle init --type basic mkdir -p src/main/java src/main/resources
Skopiuj
service_account_key.json
pobrany podczas tworzenia konta usługi do utworzonego wcześniej katalogusrc/main/resources/
.Otwórz domyślny plik
build.gradle
i zastąp jego zawartość tym kodem:
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' }
Krok 3. Skonfiguruj przykład
Utwórz plik o nazwie src/main/java/CustomerQuickstart.java
, skopiuj poniższy kod i zapisz go.
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()); } } }
Krok 4. Uruchom przykład
Skorzystaj z pomocy systemu operacyjnego, aby uruchomić skrypt w pliku. Na komputerach UNIX i Mac wykonaj w terminalu polecenie poniżej:
gradle -q run
Uwagi
- Unikaj udostępniania pliku (
service_account_key.json
). Uważaj, aby nie umieścić go w repozytoriach kodu źródłowego. Możesz dowiedzieć się więcej o obsłudze obiektów tajnych konta usługi.