按照本快速入门指南中的步骤,大约 10 分钟后,您将构建一个简单的 .NET C# 控制台应用,用于向零触摸注册客户 API 发出请求。
前提条件
如需运行本快速入门,您需要执行以下操作:
- Google 帐号,也就是您的零触摸注册客户帐号的成员。请参阅客户帐号。
- Visual Studio 2013 或更高版本。
- 能够访问互联网和网络浏览器。
第 1 步:启用 Zero-Touch Enrollment API
- 可以使用此向导在 Google Developers Console 中创建或选择项目,并自动启用此 API。点击继续,然后点击转到凭据。
- 点击“创建凭据”上的取消。
- 在页面顶部,选择 OAuth 同意屏幕标签页。选择电子邮件地址,输入产品名称(如果尚未设置),然后点击保存按钮。
- 选择凭据标签页,点击创建凭据按钮,然后选择 OAuth 客户端 ID。
- 选择应用类型其他,输入名称“快速入门”,然后点击创建按钮。
- 点击确定以关闭 OAuth 客户端面板。
- 点击 下载 JSON。
- 将文件移至工作目录,并将其重命名为
client_secret.json
。
第 2 步:准备项目
- 在 Visual Studio 中创建一个新的 .NET Core C# Console 应用项目。
- 打开软件包管理器,选择软件包来源 nuget.org,然后添加以下软件包:
Google.Apis.AndroidProvisioningPartner.v1
Google.Apis.Auth
如需了解详情,请参阅 Microsoft 文档安装和使用软件包。
第 3 步:设置示例
- 将
client_secret.json
(在第 1 步中下载)拖动到您的 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 工具栏中的
开始。首次运行该应用时,您需要授予访问权限:
- 该应用会尝试在您的默认浏览器中打开一个新的标签页。如果失败了,请从控制台中复制网址并在浏览器中打开网址。如果您尚未登录 Google 帐号,系统会提示您登录。如果您登录了多个 Google 帐号,该页面会提示您选择用于授权的帐号。
- 点击接受。
- 关闭浏览器标签页 - 应用会继续运行。
备注
- 由于 Google API 客户端库会将授权数据存储在文件系统中,因此后续启动不会提示您进行授权。
- 如需重置应用的授权数据,请删除
~/.credentials/zero-touch.quickstart.json
文件,然后重新运行应用。 - 本快速入门中的授权流程非常适合命令行应用。如需了解如何向 Web 应用添加授权,请参阅 使用 OAuth 2.0、Web 应用 (ASP.NET MVC)。
问题排查
以下是您需要检查的一些常见事项。 请 将快速入门中存在的问题告诉我们,我们将努力解决该问题。
- 确保您授权的 API 调用与您的零触摸注册客户帐号中的成员帐号所用的 Google 帐号相同。请尝试使用同一 Google 帐号登录零触摸注册门户,以测试您的访问权限。
- 确认相关帐号已在门户中接受最新的服务条款。请参阅 客户帐号。