מדריך למתחילים בנושא .NET ללקוחות

במדריך למתחילים הזה תלמדו איך, ותוך 10 דקות, תהיה לכם אפליקציה פשוטה של מסוף .NET C# ששולחת בקשות ל-API של ההרשמה דרך הארגון דרך חשבון שירות.

דרישות מוקדמות

כדי להפעיל את המדריך למתחילים, צריך:

  • חשבון שירות שמקושר לחשבון הלקוח בהרשמה דרך הארגון. קראו את המאמר תחילת העבודה.
  • Visual Studio 2013 ואילך.
  • גישה לאינטרנט ולדפדפן אינטרנט.

שלב 1: הפעלת ה-API של ההרשמה דרך הארגון

  1. בעזרת האשף הזה אפשר ליצור או לבחור פרויקט ב-Google Developers Console ולהפעיל את ה-API באופן אוטומטי. לוחצים על Continue (המשך), ואז על Credentials .
  2. מגדירים שאל אילו נתונים נכנסים? נתוני אפליקציה.
  3. לוחצים על הבא. אמורה להופיע בקשה ליצור חשבון שירות.
  4. נותנים שם תיאורי לשם חשבון השירות.
  5. כדאי לרשום את מספר חשבון השירות (נראה כמו כתובת אימייל), כי הוא ישמש אותך בהמשך.
  6. מגדירים את התפקיד כחשבונות שירות > משתמש בחשבון שירות.
  7. לוחצים על Done כדי לסיים ליצור את חשבון השירות.
  8. לוחצים על כתובת האימייל של חשבון השירות שנוצר.
  9. לוחצים על **מקשים**.
  10. לוחצים על **הוספת מפתח** ואז על **יצירת מפתח חדש**.
  11. עבור **סוג מפתח**, בוחרים באפשרות **JSON**.
  12. לוחצים על יצירה והמפתח הפרטי יורד למחשב.
  13. לוחצים על **סגירה**.
  14. מעבירים את הקובץ לספריית העבודה ומשנים את השם שלו ל-service_account_key.json.

שלב 2: הכנת הפרויקט

  1. יוצרים פרויקט Console Application חדש ב-NET .C# ב-Visual Studio.
  2. פותחים את מנהל החבילות, בוחרים את מקור החבילה nuget.org ומוסיפים את החבילות הבאות:
    • Google.Apis.AndroidProvisioningPartner.v1
    • Google.Apis.Auth

למידע נוסף, קראו את המאמר התקנה ושימוש בחבילה של Microsoft.

שלב 3: הגדרת הדוגמה

  1. גוררים את קובץ ה-service_account_key.json שהורדתם כשיצרתם את חשבון השירות לסייר הפתרונות החזותיים של Studio.
  2. בוחרים ב-service_account_key.json, עוברים לחלון 'מאפיינים' ומגדירים את השדה העתקה לספריית הפלט כתמיד להעתיק.
  3. מחליפים את התוכן של 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.

הערות

מידע נוסף