Bu hızlı başlangıç kılavuzundaki adımları uygulayarak yaklaşık 10 dakikada, hizmet hesabı kullanarak sıfır dokunuşla kayıt müşteri API'sine istek gönderen basit bir Java komut satırı uygulaması oluşturabilirsiniz.
Ön koşullar
Bu hızlı başlangıç kılavuzunu çalıştırmak için şunlara ihtiyacınız vardır:
- El değmeden kayıt müşteri hesabınıza bağlı bir hizmet hesabı. Başlayın başlıklı makaleyi inceleyin.
- Java 1.7 veya üzeri.
- Gradle 2.3 veya sonraki sürümler.
- İnternete ve web tarayıcıya erişim.
1. adım: El değmeden kayıt API'sini etkinleştirin
- Bunu kullanın sihirbazı kullanarak Google Developers Console'da proje oluşturabilir veya seçebilirsiniz ve API'yi otomatik olarak açabilir. Devam ve ardından Kimlik bilgilerine git'i tıklayın ziyaret edin.
- Hangi verilere erişeceksiniz? alanını Uygulama verileri olarak ayarlayın.
- İleri'yi tıklayın. Hizmet oluşturmanız istenecektir hesap.
- Hizmet hesabı adı için açıklayıcı bir ad verin.
- Daha sonra kullanacağınız için Hizmet hesabı kimliğini (e-posta adresine benzer) not edin.
- Rol'ü Hizmet Hesapları > Hizmet Hesabı Kullanıcısı.
- Hizmet hesabını oluşturmayı tamamlamak için Bitti'yi tıklayın.
- Oluşturduğunuz hizmet hesabının e-posta adresini tıklayın.
- **Anahtarlar**'ı tıklayın.
- **Anahtar ekle**'yi, ardından **Yeni anahtar oluştur**'u tıklayın.
- **Anahtar türü** için **JSON**'yi seçin.
- Oluştur'u tıklayın. Özel anahtar bilgisayarınıza indirilir.
- **Kapat**'ı tıklayın.
- Dosyayı çalışma dizininize taşıyın ve
service_account_key.json
olarak yeniden adlandırın.
2. Adım: Projeyi hazırlayın
Gradle projenizi ayarlamak için aşağıdaki adımları uygulayın:
Çalışma dizininde yeni bir proje oluşturmak için aşağıdaki komutu çalıştırın:
gradle init --type basic mkdir -p src/main/java src/main/resources
Hizmet hesabınızı oluştururken indirdiğiniz
service_account_key.json
dosyasını, yukarıda oluşturduğunuzsrc/main/resources/
dizinine kopyalayın.Varsayılan
build.gradle
dosyasını açın ve içeriğini aşağıdaki kodla değiştirin:
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' }
3. Adım: Örneği ayarlayın
src/main/java/CustomerQuickstart.java
adında bir dosya oluşturun ve dosyayı
ve dosyayı kaydedin.
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()); } } }
4. Adım: Sana Özel'i çalıştırın
Dosyadaki komut dosyasını çalıştırmak için işletim sisteminizin yardım sayfasını kullanın. UNIX ve Mac bilgisayarlarda terminalinizde aşağıdaki komutu çalıştırın:
gradle -q run
Notlar
service_account_key.json
dosyanızı kimseyle paylaşmayın. Bu bilgileri kaynak kod depolarına dahil etmemeye dikkat edin. Hizmet hesabı gizli bilgilerini yönetme hakkında daha fazla öneriyi okuyabilirsiniz.