面向转销商的 .NET 快速入门

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

前提条件

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

  • 一个 Google 账号(属于您的零触摸注册转销商的成员) 。如果您尚未完成初始配置,请按照使用入门中的步骤操作 转销商门户指南
  • Visual Studio 2013 或更高版本。
  • 能够访问互联网和网络浏览器。

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

  1. 使用此 向导,以在 Google Developers Console 中创建或选择项目。 系统会自动启用该 API。点击继续,然后点击前往凭据页面
  2. 您要访问哪些数据?设置为应用数据
  3. 点击下一步。系统应该会提示您创建 Service 。
  4. 服务账号名称指定一个描述性名称。
  5. 记下服务账号 ID(看起来像是电子邮件地址),因为您将 稍后使用。
  6. 角色设置为服务账号 >Service Account User
  7. 点击完成以完成服务账号的创建过程。
  8. 点击您创建的服务账号的电子邮件地址。
  9. 点击 **Keys**。
  10. 点击 **Add key**,然后点击 **Create new key**。
  11. 对于 **密钥类型**,选择 **JSON**。
  12. 点击创建,私钥便会下载到您的计算机。
  13. 点击 **Close**。
  14. 将文件移动到工作目录,并将其重命名为 service_account_key.json
  1. 打开零触摸注册门户。您可能需要登录。
  2. 点击 服务 账号
  3. 点击 关联服务账号
  4. 电子邮件地址设置为您创建的服务账号的地址。
  5. 点击关联服务账号,通过零触摸功能使用服务账号 注册账号。

第 3 步:准备项目

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

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

第 4 步:设置示例

  1. 将第 1 步中下载的 service_account_key.json 拖动到您的可视化图表中 Studio 解决方案资源管理器。
  2. 选择 service_account_key.json,然后转到“Properties”(属性)窗口 将 Copy to output directory 字段设为 Always copy
  3. Program.cs 的内容替换为以下代码。
  4. 插入您自己的转销商合作伙伴 ID 作为 PartnerId(应用的第一行)。
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;

namespace ZeroTouchResellerQuickstart
{
    class Program
    {
        // TODO: replace this with your partner reseller ID.
        static long PartnerId = 11036885;

        // Use a single scope for the all methods in the reseller API.
        static readonly string[] Scopes =
        { "https://www.googleapis.com/auth/androidworkprovisioning" };
        static string ApplicationName = "Zero-touch Reseller .NET Quickstart";

        static void Main(string[] args)
        {
            // Create a credential to authorize API requests using a service account key.
            // The service account must be linked using the zero-touch portal.
            ServiceAccountCredential credential;
            using (var stream =
                new FileStream("service_account_key.json", FileMode.Open, FileAccess.Read))
            {
                credential = GoogleCredential.FromStream(stream)
                                     .CreateScoped(Scopes)
                                     .UnderlyingCredential as ServiceAccountCredential;
            }

            // Create a zero-touch enrollment API service endpoint.
            var service = new AndroidProvisioningPartnerService(new BaseClientService.Initializer
            {
                HttpClientInitializer = credential,
                ApplicationName = ApplicationName
            });

            // Send an API request to list all our customers.
            PartnersResource.CustomersResource.ListRequest request =
                service.Partners.Customers.List(PartnerId);
            ListCustomersResponse response = request.Execute();

            // Print out the details of each customer.
            IList<Company> customers = response.Customers;
            if (customers != null)
            {
                foreach (Company customer in customers)
                {
                    Console.WriteLine("Name:{0}  ID:{1}",
                                      customer.CompanyName,
                                      customer.CompanyId);
                }
            }
            else
            {
                Console.WriteLine("No customers found");
            }
        }
    }
}

合作伙伴 ID

调用 API 时,通常需要将您的转销商合作伙伴 ID 作为参数。要查找您的 合作伙伴 ID,请按以下步骤操作:

  1. 打开门户网站。您可能需要登录。
  2. 点击 服务 账号
  3. 复制您的转销商 ID 行中的合作伙伴 ID。

第 5 步:运行示例代码

如需构建并运行示例,请点击 Visual Studio 工具栏中的 Start

问题排查

告诉我们您在快速入门时遇到了什么问题,我们将努力 解决问题。如需了解零触摸服务如何使用服务账号向 API 调用授权,请阅读 授权

了解详情