ทําตามขั้นตอนในคู่มือเริ่มใช้งานฉบับย่อนี้ และในอีก 10 นาทีโดยประมาณ คุณมีแอปคอนโซล .NET C# ง่ายๆ ที่ส่งคําขอไปยัง API ลูกค้าการตั้งค่าอุปกรณ์พร้อมใช้แบบรวมกลุ่ม โดยใช้บัญชีบริการ
สิ่งที่ต้องดำเนินการก่อน
หากต้องการเรียกใช้การเริ่มต้นอย่างรวดเร็วนี้ คุณต้องมีสิ่งต่อไปนี้
- บัญชีบริการที่ลิงก์กับบัญชีลูกค้าการตั้งค่าอุปกรณ์พร้อมใช้แบบรวมกลุ่มของคุณ โปรดดูเริ่มต้นใช้งาน
- Visual Studio 2013 หรือใหม่กว่า
- เข้าถึงอินเทอร์เน็ตและเว็บเบราว์เซอร์
ขั้นตอนที่ 1: เปิด API การตั้งค่าอุปกรณ์พร้อมใช้แบบรวมกลุ่ม
- ใช้วิซาร์ดนี้เพื่อสร้างหรือเลือกโปรเจ็กต์ใน Google Developers Console และเปิด API โดยอัตโนมัติ คลิกต่อไป แล้วคลิกไปที่ข้อมูลรับรอง
- กําหนดคุณจะเข้าถึงข้อมูลใดบ้างเป็นข้อมูลแอปพลิเคชัน
- คลิกถัดไป ระบบควรแจ้งให้สร้างบัญชีบริการ
- ตั้งชื่อที่สื่อความหมายสําหรับชื่อบัญชีบริการ
- จดรหัสบัญชีบริการ (ลักษณะเหมือนที่อยู่อีเมล) เพราะคุณจะต้องใช้รหัสในภายหลัง
- ตั้งค่าบทบาทเป็นบัญชีบริการ > ผู้ใช้บัญชีบริการ
- คลิกเสร็จสิ้นเพื่อจบการสร้างบัญชี
- คลิกที่อยู่อีเมลของบัญชีบริการที่คุณสร้างขึ้น
- คลิก **คีย์**
- คลิก **เพิ่มคีย์** จากนั้นคลิก **สร้างคีย์ใหม่**
- สําหรับ **ประเภทคีย์** ให้เลือก **JSON**
- คลิกสร้าง และการดาวน์โหลดคีย์ส่วนตัวลงในคอมพิวเตอร์
- คลิก **ปิด**
- ย้ายไฟล์ไปยังไดเรกทอรีการทํางานและเปลี่ยนชื่อ
service_account_key.json
ขั้นตอนที่ 2: จัดเตรียมโครงการ
- สร้างโปรเจ็กต์ Application Console .NET Core C# ใหม่ใน Visual Studio
- เปิดตัวจัดการแพ็กเกจ เลือกแหล่งที่มาของแพ็กเกจ nuget.org และเพิ่มแพ็กเกจต่อไปนี้
Google.Apis.AndroidProvisioningPartner.v1
Google.Apis.Auth
ดูข้อมูลเพิ่มเติมได้ในเอกสาร Microsoft ติดตั้งและใช้แพ็กเกจ
ขั้นตอนที่ 3: ตั้งค่าตัวอย่าง
- ลาก
service_account_key.json
ที่คุณดาวน์โหลดเมื่อสร้างบัญชีบริการลงใน Visual Studio Solution Explorer - เลือก
service_account_key.json
จากนั้นไปที่หน้าต่างคุณสมบัติและตั้งค่าช่องคัดลอกไปยังไดเรกทอรีเอาต์พุตเป็นคัดลอกทุกครั้ง - แทนที่เนื้อหาของ
Program.cs
ด้วยโค้ดต่อไปนี้
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); } } } }
ขั้นตอนที่ 4: เรียกใช้ตัวอย่าง
หากต้องการสร้างและเรียกใช้ตัวอย่าง ให้คลิก
เริ่มต้นในแถบเครื่องมือของ Visual Studioหมายเหตุ
- หลีกเลี่ยงการแชร์ไฟล์
service_account_key.json
กับคนอื่น โปรดระวัง อย่ารวมรหัสนี้ไว้ในที่เก็บซอร์สโค้ด คุณอ่านคําแนะนําเพิ่มเติมเกี่ยวกับการจัดการข้อมูลลับของบัญชีบริการได้