按照本快速入门指南中的步骤操作,大约 10 分钟后,您将获得一个简单的 .NET C# 控制台应用,该应用可使用服务帐号向零触摸注册客户 API 发出请求。
前提条件
如需运行本快速入门,您需要执行以下操作:
- 关联到您的零触摸注册客户帐号的服务帐号。请参阅使用入门。
- Visual Studio 2013 或更高版本。
- 能够访问互联网和网络浏览器。
第 1 步:启用 Zero-Touch Enrollment API
- 可以使用此向导在 Google Developers Console 中创建或选择项目,并自动启用此 API。点击继续,然后点击转到凭据。
- 将您要访问哪些数据?设为应用数据。
- 点击 Next。系统会提示您创建服务帐号。
- 为服务帐号名称指定一个描述性名称。
- 记下服务帐号 ID(类似于电子邮件地址),因为您稍后会用到它。
- 将角色设置为服务帐号 > Service Account User。
- 点击完成以完成服务帐号的创建过程。
- 点击您创建的服务帐号的电子邮件地址。
- 点击 **密钥**。
- 点击 **添加密钥**,然后点击 **创建新密钥**。
- 对于 **密钥类型**,选择 **JSON**。
- 点击创建,私钥即会下载到您的计算机。
- 点击 **关闭**。
- 将文件移至工作目录,并将其重命名为
service_account_key.json
。
第 2 步:准备项目
- 在 Visual Studio 中创建一个新的 .NET Core C# Console 应用项目。
- 打开软件包管理器,选择软件包来源 nuget.org,然后添加以下软件包:
Google.Apis.AndroidProvisioningPartner.v1
Google.Apis.Auth
如需了解详情,请参阅 Microsoft 文档安装和使用软件包。
第 3 步:设置示例
- 将创建服务帐号时下载的
service_account_key.json
拖动到 Visual 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 步:运行示例代码
如需构建并运行示例,请点击 Visual Studio 工具栏中的
开始。备注
- 避免与任何人共享您的
service_account_key.json
文件。请注意不要将其包含在源代码库中。您可以详细了解如何处理服务帐号密钥。