ทําตามขั้นตอนในคู่มือเริ่มใช้งานฉบับย่อนี้ และในอีก 10 นาทีโดยประมาณ คุณมีแอปคอนโซล .NET C# ง่ายๆ ที่ส่งคําขอไปยัง API ลูกค้าการตั้งค่าอุปกรณ์พร้อมใช้แบบรวมกลุ่ม
สิ่งที่ต้องดำเนินการก่อน
หากต้องการเรียกใช้การเริ่มต้นอย่างรวดเร็วนี้ คุณต้องมีสิ่งต่อไปนี้
- บัญชี Google ที่เป็นสมาชิกของบัญชีลูกค้า การตั้งค่าอุปกรณ์พร้อมใช้แบบรวมกลุ่ม ดูบัญชีลูกค้า
- Visual Studio 2013 หรือใหม่กว่า
- เข้าถึงอินเทอร์เน็ตและเว็บเบราว์เซอร์
ขั้นตอนที่ 1: เปิด API การตั้งค่าอุปกรณ์พร้อมใช้แบบรวมกลุ่ม
- ใช้วิซาร์ดนี้เพื่อสร้างหรือเลือกโปรเจ็กต์ใน Google Developers Console และเปิด API โดยอัตโนมัติ คลิกต่อไป แล้วคลิกไปที่ข้อมูลรับรอง
- คลิกยกเลิกที่ข้อมูลรับรองการสร้าง
- เลือกแท็บหน้าจอคํายินยอม OAuth ที่ด้านบนของหน้า เลือกที่อยู่อีเมล ป้อนชื่อผลิตภัณฑ์หากยังไม่ได้ตั้งค่าไว้ แล้วคลิกปุ่มบันทึก
- เลือกแท็บข้อมูลเข้าสู่ระบบ คลิกปุ่มสร้างข้อมูลเข้าสู่ระบบ และเลือกรหัสไคลเอ็นต์ OAuth
- เลือกประเภทแอปพลิเคชันเป็นอื่นๆ จากนั้นป้อนชื่อ "Quickstart" และคลิกปุ่มสร้าง
- คลิกตกลงเพื่อปิดแผงไคลเอ็นต์ OAuth
- คลิก ดาวน์โหลด JSON
- ย้ายไฟล์ไปยังไดเรกทอรีการทํางานและเปลี่ยนชื่อ
client_secret.json
ขั้นตอนที่ 2: จัดเตรียมโครงการ
- สร้างโปรเจ็กต์ Application Console .NET Core C# ใหม่ใน Visual Studio
- เปิดตัวจัดการแพ็กเกจ เลือกแหล่งที่มาของแพ็กเกจ nuget.org และเพิ่มแพ็กเกจต่อไปนี้
Google.Apis.AndroidProvisioningPartner.v1
Google.Apis.Auth
ดูข้อมูลเพิ่มเติมได้ในเอกสาร Microsoft ติดตั้งและใช้แพ็กเกจ
ขั้นตอนที่ 3: ตั้งค่าตัวอย่าง
- ลาก
client_secret.json
(ที่ดาวน์โหลดมาในขั้นตอนที่ 1) ไปยัง Visual Explorer Visual Studio - เลือก
client_secret.json
จากนั้นไปที่หน้าต่างพร็อพเพอร์ตี้และตั้งค่าช่องคัดลอกไปยังไดเรกทอรีเอาต์พุตเป็นคัดลอกทุกครั้ง - แทนที่เนื้อหาของ
Program.cs
ด้วยโค้ดต่อไปนี้
using Google.Apis.AndroidProvisioningPartner.v1; using Google.Apis.AndroidProvisioningPartner.v1.Data; using Google.Apis.Auth.OAuth2; using Google.Apis.Services; using Google.Apis.Util.Store; 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) { UserCredential credential; // Ask the user to authorize the request using their Google Account // in their browser. using (var stream = new FileStream("client_secret.json", FileMode.Open, FileAccess.Read)) { string credPath = System.Environment.GetFolderPath( System.Environment.SpecialFolder.Personal); credPath = Path.Combine(credPath, ".credentials/zero-touch.quickstart.json"); credential = GoogleWebAuthorizationBroker.AuthorizeAsync( GoogleClientSecrets.FromStream(stream).Secrets, Scopes, "user", CancellationToken.None, new FileDataStore(credPath, true)).Result; Console.WriteLine("Credential file saved to: " + credPath); } // 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คุณจะต้องให้สิทธิ์ในการเข้าถึงเมื่อเรียกใช้แอปเป็นครั้งแรก ดังนี้
- แอปพลิเคชันจะพยายามเปิดแท็บใหม่ในเบราว์เซอร์เริ่มต้นของคุณ หากไม่สําเร็จ ให้คัดลอก URL จากคอนโซลและเปิดในเบราว์เซอร์ หากคุณยังไม่ได้เข้าสู่ระบบบัญชี Google ระบบจะขอให้คุณลงชื่อเข้าสู่ระบบ หากคุณเข้าสู่ระบบบัญชี Google หลายบัญชี หน้านี้จะแสดงขึ้นมาให้คุณเลือกบัญชีที่จะให้สิทธิ์
- คลิกยอมรับ
- ปิดแท็บเบราว์เซอร์ แอปจะยังคงทํางานต่อไป
หมายเหตุ
- เนื่องจากไลบรารีของไคลเอ็นต์ Google API จัดเก็บข้อมูลการให้สิทธิ์ไว้ในระบบไฟล์ การเปิดใช้งานครั้งต่อๆ ไปจึงไม่แจ้งให้คุณให้สิทธิ์
- หากต้องการรีเซ็ตข้อมูลการให้สิทธิ์ของแอป ให้ลบไฟล์
~/.credentials/zero-touch.quickstart.json
และเรียกใช้แอปอีกครั้ง - ขั้นตอนการให้สิทธิ์ในการเริ่มต้นอย่างรวดเร็วนี้เหมาะสําหรับแอปบรรทัดคําสั่ง หากต้องการดูวิธีเพิ่มการให้สิทธิ์ไปยังเว็บแอป ให้ดูที่ การใช้ OAuth 2.0 เว็บแอปพลิเคชัน (ASP.NET MVC)
การแก้ปัญหา
คุณจะต้องตรวจสอบสิ่งทั่วไปต่อไปนี้ โปรดแจ้งให้เราทราบว่ามีอะไรผิดปกติจากการเริ่มต้นอย่างรวดเร็ว เพื่อให้เราแก้ไขปัญหาดังกล่าว
- โปรดตรวจสอบว่าคุณให้สิทธิ์การเรียก API ด้วยบัญชี Google เดียวกับที่เป็นสมาชิกของบัญชีลูกค้า การตั้งค่าอุปกรณ์พร้อมใช้แบบรวมกลุ่ม ลองลงชื่อเข้าใช้พอร์ทัลการตั้งค่าอุปกรณ์พร้อมใช้แบบรวมกลุ่มโดยใช้บัญชี Google เดียวกันเพื่อทดสอบการเข้าถึง
- ตรวจสอบว่าบัญชียอมรับข้อกําหนดในการให้บริการล่าสุดในพอร์ทัลแล้ว โปรดดู บัญชีลูกค้า