Ikuti langkah-langkah dalam panduan memulai ini, dan dalam waktu sekitar 10 menit, Anda memiliki aplikasi konsol .NET C# sederhana yang membuat permintaan ke API pelanggan pendaftaran zero-touch menggunakan akun layanan.
Prasyarat
Untuk menjalankan panduan memulai ini, Anda memerlukan:
- Akun layanan, yang ditautkan ke akun pelanggan pendaftaran zero-touch Anda. Lihat Memulai.
- Visual Studio 2013 atau yang lebih baru.
- Akses ke internet dan browser web.
Langkah 1: Aktifkan API pendaftaran zero-touch
- Gunakan wizard ini untuk membuat atau memilih project di Google Developers Console dan otomatis mengaktifkan API. Klik Continue, lalu Go to credentials .
- Setel Data apa yang akan Anda akses? ke Data aplikasi.
- Klik Berikutnya. Anda akan diminta untuk membuat akun layanan.
- Beri nama deskriptif untuk Nama akun layanan.
- Catat ID akun layanan (formatnya seperti alamat email) karena Anda akan menggunakannya nanti.
- Tetapkan Role ke Service Accounts > Service Account User.
- Klik Selesai untuk menyelesaikan pembuatan akun layanan.
- Klik alamat email untuk akun layanan yang dibuat.
- Klik **Kunci**.
- Klik **Add key**, lalu klik **Create new key**.
- Untuk **Key type**, pilih **JSON**.
- Klik Create dan kunci pribadi akan didownload ke komputer Anda.
- Klik **Tutup**.
- Pindahkan file ke direktori kerja Anda dan ganti namanya menjadi
service_account_key.json
.
Langkah 2: Siapkan project
- Buat project Console Console dari Aplikasi .NET Core C# baru di Visual Studio.
- Buka Pengelola Paket, pilih sumber paket nuget.org, lalu tambahkan
paket berikut:
Google.Apis.AndroidProvisioningPartner.v1
Google.Apis.Auth
Untuk mempelajari lebih lanjut, baca dokumen Microsoft Menginstal dan menggunakan paket.
Langkah 3: Siapkan contoh file
- Tarik
service_account_key.json
yang didownload saat membuat akun layanan ke Penjelajah Solusi Visual Studio. - Pilih
service_account_key.json
, lalu buka jendela Properties, dan tetapkan kolom Copy to output direktori ke Selalu salin. - Ganti konten
Program.cs
dengan kode berikut:
using Google.Apis.AndroidProvisioningPartner.v1; using Google.Apis.AndroidProvisioningPartner.v1.Data; using Google.Apis.Auth.OAuth2; using Google.Apis.Services; using System; using System.Collections.Generic; using System.IO; using System.Threading; namespace ZeroTouchCustomerQuickstart { class Program { // A single scope is used for the zero-touch enrollment customer API. static readonly string[] Scopes = { "https://www.googleapis.com/auth/androidworkzerotouchemm" }; static string ApplicationName = "Zero-touch Enrollment .NET Quickstart"; static void Main(string[] args) { GoogleCredential credential; // Authenticate using the service account key credential = GoogleCredential.FromFile("service_account_key.json") .CreateScoped(Scopes); // Create a zero-touch enrollment API service endpoint. var service = new AndroidProvisioningPartnerService(new BaseClientService.Initializer { HttpClientInitializer = credential, ApplicationName = ApplicationName }); // Get the customer's account. Because a customer might have more // than one, limit the results to the first account found. CustomersResource.ListRequest accountRequest = service.Customers.List(); accountRequest.PageSize = 1; CustomerListCustomersResponse accountResponse = accountRequest.Execute(); if (accountResponse.Customers.Count == 0) { // No accounts found for the user. Confirm the Google Account // that authorizes the request can access the zero-touch portal. Console.WriteLine("No zero-touch enrollment account found."); Environment.Exit(-1); } Company customer = accountResponse.Customers[0]; var customerAccount = String.Format("customers/{0}", customer.CompanyId); // Send an API request to list all the DPCs available. CustomersResource.DpcsResource.ListRequest request = service.Customers.Dpcs. List(customerAccount); CustomerListDpcsResponse response = request.Execute(); // Print out the details of each DPC. IList<Dpc> dpcs = response.Dpcs; foreach (Dpc dpcApp in dpcs) { Console.WriteLine("Name:{0} APK:{1}", dpcApp.DpcName, dpcApp.PackageName); } } } }
Langkah 4: Jalankan contoh aplikasi
Untuk membuat dan menjalankan contoh, klik
Start di toolbar Visual Studio.Catatan
- Jangan membagikan file
service_account_key.json
Anda kepada siapa pun. Berhati-hatilah untuk tidak menyertakannya dalam repositori kode sumber. Anda dapat membaca saran selengkapnya tentang menangani rahasia akun layanan.