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