面向客户的 .NET 快速入门

按照本快速入门指南中的步骤操作,在大约 10 分钟内 一个简单的 .NET C# 控制台应用,用于请求零触摸注册 客户 API。

前提条件

如需运行本快速入门,您需要:

  • Google 账号(属于您的零触摸注册客户的成员) 。请参阅客户 账号。
  • Visual Studio 2013 或更高版本。
  • 能够访问互联网和网络浏览器。

第 1 步:启用零触摸注册 API

  1. 使用此 向导,以在 Google Developers Console 中创建或选择项目。 系统会自动启用该 API。点击继续,然后点击前往凭据页面 。
  2. 在“创建凭据”页面上点击取消。
  3. 在页面顶部,选择 OAuth 同意屏幕标签页。请选择一个 Email address(电子邮件地址),输入 Product name(如果尚未设置),并且 点击保存按钮。
  4. 选择凭据标签页,然后点击创建凭据。 按钮,然后选择 OAuth 客户端 ID。
  5. 选择应用类型其他,然后输入名称 “快速入门”,然后点击创建 按钮。
  6. 点击确定以关闭 OAuth 客户端面板。
  7. 点击 下载 JSON。
  8. 将文件移动到工作目录,并将其重命名为 client_secret.json。

第 2 步:准备项目

  1. 在 Visual Studio 中创建新的 .NET Core C# 控制台应用项目。
  2. 打开软件包管理器,选择软件包源 nuget.org,然后添加 以下软件包: <ph type="x-smartling-placeholder">
      </ph>
    • Google.Apis.AndroidProvisioningPartner.v1
    • Google.Apis.Auth

有关详情,请参阅 Microsoft 文档安装和使用 软件包。

第 3 步:设置示例

  1. 将第 1 步中下载的 client_secret.json 拖动到 Visual Studio 中 解决方案资源管理器。
  2. 选择 client_secret.json,然后转到“Properties”窗口并设置 Copy to output directory 字段改为 Always copy。
  3. 将 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 工具栏中的 Start。

首次运行应用时,您需要授予访问权限:

  1. 应用会尝试在您的默认浏览器中打开新标签页。如果此操作失败,请从 控制台,并在浏览器中将其打开。如果您还没有登录 Google 账号, 提示登录。如果您登录了多个 Google 账号,页面会提示您选择 用于授权的账号。
  2. 点击接受。
  3. 关闭浏览器标签页,应用会继续运行。

备注

  • 由于 Google API 客户端库在文件系统中存储授权数据,因此后续 也不会提示您进行授权
  • 要重置应用的授权数据,请删除 ~/.credentials/zero-touch.quickstart.json 文件,然后再次运行应用。
  • 本快速入门中的授权流程非常适合命令行应用。如需了解如何添加 授权,请参阅 使用 OAuth 2.0、Web 应用 (ASP.NET MVC)。

问题排查

以下是您需要检查的一些常见事项。 <ph type="x-smartling-placeholder"></ph> 请将快速入门中的问题告诉我们,我们将努力解决此问题。

  • 确保您在授权 API 调用时使用的 Google 账号是您 零触摸注册客户账号。尝试使用以下网址登录零触摸注册门户: 请使用同一个 Google 账号测试您的访问权限。
  • 确认账号已接受最新版《服务条款》 门户网站。请参阅 客户账号。

了解详情