面向转销商的 .NET 快速入门
使用集合让一切井井有条
根据您的偏好保存内容并对其进行分类。
按照本快速入门指南中的步骤操作,在大约 10 分钟内,您将获得一个简单的 .NET C# 控制台应用,该应用可向零触摸注册转销商 API 发出请求。
前提条件
如需运行本快速入门,您需要满足以下条件:
- 一个 Google 账号,该账号是您的“零触摸注册”转销商账号的成员。如果您尚未完成新手入门流程,请按照转销商门户指南中的使用入门部分中的步骤操作。
- Visual Studio 2013 或更高版本。
- 能够连接到互联网并使用网络浏览器。
第 1 步:启用零触摸注册 API
- 使用此向导在 Google Developers Console 中创建或选择项目,并自动启用该 API。点击继续,然后点击转到凭据。
- 将您要访问哪些数据?设置为应用数据。
- 点击下一步。系统应该会提示您创建服务账号。
- 为服务账号名称提供一个描述性名称。
- 记下服务账号 ID(看起来像电子邮件地址),因为您稍后将用到它。
- 将角色设置为 Service Accounts > Service Account User。
- 点击完成以完成服务账号的创建过程。
- 点击您创建的服务账号的电子邮件地址。
- 点击**密钥**。
- 点击**添加密钥**,然后点击**创建新密钥**。
- 在“密钥类型”中,选择“JSON”。
- 点击创建,私钥便会下载到您的计算机。
- 点击“关闭”。
- 将该文件移至您的工作目录中,并将其重命名为
service_account_key.json
。
第 2 步:关联服务账号
- 打开零触摸注册门户。您可能需要登录。
- 点击 settings_ethernet 服务账号。
- 点击 add 关联服务账号。
- 将电子邮件地址设置为您创建的服务账号的地址。
- 点击关联服务账号,将服务账号与零触摸注册账号搭配使用。
第 3 步:准备项目
- 在 Visual Studio 中创建一个新的 .NET Core C# 控制台应用项目。
- 打开软件包管理器,选择软件包来源 nuget.org,然后添加以下软件包:
Google.Apis.AndroidProvisioningPartner.v1
Google.Apis.Auth
如需了解详情,请参阅 Microsoft 文档安装和使用软件包。
第 4 步:设置示例
- 将第 1 步中下载的
service_account_key.json
拖动到 Visual Studio Solution Explorer 中。
- 选择
service_account_key.json
,然后前往“Properties”(属性)窗口,将 Copy to output directory(复制到输出目录)字段设置为 Always copy(始终复制)。
- 将
Program.cs
的内容替换为以下代码。
- 插入您自己的转销商合作伙伴 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,请按以下步骤操作:
- 打开门户网站。您可能需要登录。
- 点击 settings_ethernet 服务账号。
- 复制您的转销商 ID 行中的合作伙伴 ID。
第 5 步:运行示例
如需构建并运行示例,请点击 Visual Studio 工具栏中的 play_arrow
Start。
问题排查
告诉我们快速入门出现了什么问题,我们会努力解决。如需了解零触摸如何使用服务账号授权 API 调用,请参阅授权。
了解详情
如未另行说明,那么本页面中的内容已根据知识共享署名 4.0 许可获得了许可,并且代码示例已根据 Apache 2.0 许可获得了许可。有关详情,请参阅 Google 开发者网站政策。Java 是 Oracle 和/或其关联公司的注册商标。
最后更新时间 (UTC):2025-08-29。
[null,null,["最后更新时间 (UTC):2025-08-29。"],[[["\u003cp\u003eThis quickstart guide helps you set up a .NET C# console application to interact with the zero-touch enrollment reseller API in approximately 10 minutes.\u003c/p\u003e\n"],["\u003cp\u003eYou'll need a Google account associated with your zero-touch enrollment reseller account, Visual Studio 2013 or later, and internet access to get started.\u003c/p\u003e\n"],["\u003cp\u003eThe process involves enabling the zero-touch enrollment API, creating and linking a service account, setting up a .NET project, and configuring the sample code with your reseller partner ID.\u003c/p\u003e\n"],["\u003cp\u003eA service account is created and linked in order to authorize API requests; service account keys must be carefully managed to maintain security.\u003c/p\u003e\n"],["\u003cp\u003eThe guide provides steps to build and run a sample application that lists your customers using the zero-touch enrollment reseller API.\u003c/p\u003e\n"]]],["This guide outlines creating a .NET C# console app to interact with the zero-touch enrollment reseller API. Key steps include: enabling the API via the Google Developers Console, creating and downloading a JSON service account key, and linking the service account in the zero-touch enrollment portal. Next, it requires creating a new .NET Core console application, adding necessary NuGet packages, and copying the service account key to the project. Finally, replace the code with the provided sample code including the partner ID, and then run the app.\n"],null,["# .NET quickstart for resellers\n\nFollow the steps in this quickstart guide, and in about 10 minutes you'll have\na simple .NET C# console app that makes requests to the zero-touch enrollment\nreseller API.\n\nPrerequisites\n-------------\n\nTo run this quickstart, you'll need:\n\n- A Google account, that's a member of your zero-touch enrollment reseller account. If you haven't onboarded yet, follow the steps in *Get started* in the [Reseller portal guide](/zero-touch/guides/portal).\n- Visual Studio 2013 or later.\n- Access to the internet and a web browser.\n\nStep 1: Turn on the zero-touch enrollment API\n---------------------------------------------\n\n1. Use [this\n wizard](https://console.developers.google.com/start/api?id=androiddeviceprovisioning.googleapis.com) to create or select a project in the Google Developers Console and automatically turn on the API. Click **Continue** , then **Go to credentials**.\n2. Set **What data will you be accessing?** to *Application data*.\n3. Click **Next**. You should be prompted to create a service account.\n4. Give a descriptive name for **Service account name**.\n5. Note the **Service account ID** (it looks like an email address) because you'll use it later.\n6. Set **Role** to *Service Accounts \\\u003e Service Account User*.\n7. Click **Done** to finish creating the service account.\n8. Click the email address for the service account that you created.\n9. Click \\*\\*Keys\\*\\*.\n10. Click \\*\\*Add key\\*\\*, then click \\*\\*Create new key\\*\\*.\n11. For \\*\\*Key type\\*\\*, select \\*\\*JSON\\*\\*.\n12. Click **Create** and the private key downloads to your computer.\n13. Click \\*\\*Close\\*\\*.\n14. Move the file to your working directory and rename it `service_account_key.json`.\n\n| **Warning:** Service account keys can become a security risk if not managed carefully. For advice see [best practices for managing API keys](https://cloud.google.com/iam/docs/best-practices-for-managing-service-account-keys).\n\nStep 2: Link the service account\n--------------------------------\n\n1. Open the zero-touch enrollment [portal](https://enterprise.google.com/android/zero-touch/resellers). You might need to sign in.\n2. Click settings_ethernet **Service\n accounts**.\n3. Click add **Link service account**.\n4. Set **Email address** to the address of the service account you created.\n5. Click **Link service account** to use the service account with your zero-touch enrollment account.\n\nStep 3: Prepare the project\n---------------------------\n\n1. Create a new .NET Core C# *Console Application* project in Visual Studio.\n2. Open the Package Manager, select the package source **nuget.org** , and add the following packages:\n - `Google.Apis.AndroidProvisioningPartner.v1`\n - `Google.Apis.Auth`\n\nTo learn more, read the Microsoft document [Install and use a\npackage](https://docs.microsoft.com/en-us/nuget/quickstart/use-a-package).\n\nStep 4: Set up the sample\n-------------------------\n\n1. Drag `service_account_key.json` (downloaded in Step 1) into your Visual Studio Solution Explorer.\n2. Select `service_account_key.json`, and then go to the Properties window and set **Copy to output directory** field to *Always copy*.\n3. Replace the contents of `Program.cs` with the following code.\n4. Insert your own [reseller partner ID](#partner-id) as the value for `PartnerId` (the app's first line).\n\n```c#\nusing Google.Apis.AndroidProvisioningPartner.v1;\nusing Google.Apis.AndroidProvisioningPartner.v1.Data;\nusing Google.Apis.Auth.OAuth2;\nusing Google.Apis.Services;\nusing System;\nusing System.Collections.Generic;\nusing System.IO;\n\nnamespace ZeroTouchResellerQuickstart\n{\n class Program\n {\n // TODO: replace this with your partner reseller ID.\n static long PartnerId = 11036885;\n\n // Use a single scope for the all methods in the reseller API.\n static readonly string[] Scopes =\n { \"https://www.googleapis.com/auth/androidworkprovisioning\" };\n static string ApplicationName = \"Zero-touch Reseller .NET Quickstart\";\n\n static void Main(string[] args)\n {\n // Create a credential to authorize API requests using a service account key.\n // The service account must be linked using the zero-touch portal.\n ServiceAccountCredential credential;\n using (var stream =\n new FileStream(\"service_account_key.json\", FileMode.Open, FileAccess.Read))\n {\n credential = GoogleCredential.FromStream(stream)\n .CreateScoped(Scopes)\n .UnderlyingCredential as ServiceAccountCredential;\n }\n\n // Create a zero-touch enrollment API service endpoint.\n var service = new AndroidProvisioningPartnerService(new BaseClientService.Initializer\n {\n HttpClientInitializer = credential,\n ApplicationName = ApplicationName\n });\n\n // Send an API request to list all our customers.\n PartnersResource.CustomersResource.ListRequest request =\n service.Partners.Customers.List(PartnerId);\n ListCustomersResponse response = request.Execute();\n\n // Print out the details of each customer.\n IList\u003cCompany\u003e customers = response.Customers;\n if (customers != null)\n {\n foreach (Company customer in customers)\n {\n Console.WriteLine(\"Name:{0} ID:{1}\",\n customer.CompanyName,\n customer.CompanyId);\n }\n }\n else\n {\n Console.WriteLine(\"No customers found\");\n }\n }\n }\n}\n```\n\n### Partner ID\n\nAPI calls typically need your reseller partner ID as an argument. To find your\npartner ID from the zero-touch enrollment portal, follow the steps below:\n\n1. Open the [portal](https://enterprise.google.com/android/zero-touch/resellers). You might need to sign in.\n2. Click settings_ethernet **Service\n accounts**.\n3. Copy your partner ID number from the **Your reseller ID** line.\n\nStep 5: Run the sample\n----------------------\n\nTo build and run the sample, click play_arrow **Start** in the Visual Studio toolbar.\n\nTroubleshooting\n---------------\n\n\n[Tell us what went wrong]() with the quickstart and we'll work to\nfix it. To learn how zero-touch uses service accounts to authorize API calls, read\n[Authorization](/zero-touch/guides/auth).\n\nLearn more\n----------\n\n- [Google Developers Console help documentation](/console/help/new)\n- [Google API Client Library for .NET\n documentation](/api-client-library/dotnet)\n- [Using OAuth 2.0 for Server to Server\n Applications](/identity/protocols/OAuth2ServiceAccount)"]]