פועלים לפי השלבים במדריך למתחילים הזה, ובתוך כ-10 דקות תהיה לכם אפליקציית מסוף פשוטה ב-.NET C# ששולחת בקשות לממשק ה-API של הלקוח להרשמה ללא מגע באמצעות חשבון שירות.
דרישות מוקדמות
כדי להפעיל את המדריך למתחילים הזה, צריך:
- חשבון שירות, שמקושר ללקוח בהרשמה דרך הארגון חשבון. תחילת העבודה
- Visual Studio 2013 ואילך.
- גישה לאינטרנט ולדפדפן אינטרנט.
שלב 1: מפעילים את ה-API להרשמה דרך הארגון
- אפשר להשתמש בקישור הזה באשף ליצירה או בחירה של פרויקט ב-Google Developers Console, להפעיל את ה-API באופן אוטומטי. לוחצים על המשך ואז על כניסה לפרטי הכניסה.
- מגדירים את האפשרות אילו נתונים תיגש? לנתוני אפליקציה.
- לוחצים על הבא. אמורה להופיע בקשה ליצור שירות חשבון.
- נותנים לשם חשבון השירות שם תיאורי.
- שימו לב למזהה חשבון השירות (הוא נראה כמו כתובת אימייל) כי תצטרכו אותו בהמשך.
- מגדירים את Role ל-Service Accounts > משתמש בחשבון שירות.
- לוחצים על Done כדי לסיים ליצור את חשבון השירות.
- לוחצים על כתובת האימייל של חשבון השירות שנוצר.
- לוחצים על **Keys**.
- לוחצים על **הוספת מפתח** ואז על **יצירת מפתח חדש**.
- בשדה **סוג מפתח**, בוחרים באפשרות **JSON**.
- לוחצים על יצירה ואז על הורדת המפתח הפרטי למחשב.
- לוחצים על **סגירה**.
- מעבירים את הקובץ לספריית העבודה ומעניקים לו את השם
service_account_key.json
.
שלב 2: הכנת הפרויקט
- יוצרים פרויקט חדש של אפליקציית מסוף ב-Visual Studio ב-C# .NET Core.
- פותחים את מנהל החבילות, בוחרים את מקור החבילות nuget.org ומוסיפים את החבילות הבאות:
Google.Apis.AndroidProvisioningPartner.v1
Google.Apis.Auth
למידע נוסף, אפשר לקרוא את מסמך Microsoft התקנה ושימוש חבילה.
שלב 3: הגדרת הדוגמה
- גוררים את
service_account_key.json
שהורדתם כשיצרתם את חשבון שירות ב-Visual Studio Solution Explorer. - בוחרים באפשרות
service_account_key.json
, עוברים לחלון Properties ומגדירים את השדה Copy to output directory (העתקה לספריית הפלט) לערך Always copy (העתקה תמידית). - מחליפים את התוכן של
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: הרצת הדוגמה
כדי ליצור ולהריץ את הדוגמה, לוחצים על
Start בסרגל הכלים של Visual Studio.הערות
- חשוב לא לשתף את הקובץ
service_account_key.json
עם אף אחד. זהירות לא לכלול אותו במאגרים של קוד מקור. מידע נוסף זמין במאמר טיפול בסודות של חשבונות שירות.