經銷商專用的 .NET 快速入門導覽課程
透過集合功能整理內容
你可以依據偏好儲存及分類內容。
只要按照這份快速入門指南中的步驟操作,大約 10 分鐘內,您就能完成一個簡單的 .NET C# 主控台應用程式,向零接觸註冊經銷商 API 提出要求。
必要條件
如要執行這個快速入門,您需要:
- 您的零接觸註冊機制經銷商帳戶的成員 Google 帳戶。如果尚未完成新手上路流程,請按照經銷商入口網站指南中的入門步驟操作。
- Visual Studio 2013 以上版本。
- 可連上網際網路並具備網路瀏覽器。
步驟 1:啟用零接觸註冊 API
- 使用這個精靈,在 Google Developers Console 中建立或選取專案,並自動開啟 API。按一下「繼續」,然後點選「前往憑證」。
- 將「您需要存取什麼資料?」設為「應用程式資料」。
- 按一下「繼續」,系統應會提示您建立服務帳戶。
- 為「服務帳戶名稱」設定容易辨識的名稱。
- 請記下服務帳戶 ID (看起來像電子郵件地址),因為您稍後會用到這個 ID。
- 將「角色」設為「服務帳戶」>「服務帳戶使用者」。
- 按一下「Done」(完成),即完成建立服務帳戶。
- 點選您建立的服務帳戶的電子郵件地址。
- 按一下「金鑰」。
- 依序點選「新增金鑰」和「建立新的金鑰」。
- 在「金鑰類型」中選取「JSON」。
- 按一下「Create」,私密金鑰就會下載到電腦中。
- 按一下「關閉」。**
- 將檔案移至工作目錄,並重新命名為
service_account_key.json
。
步驟 2:連結服務帳戶
- 開啟零接觸註冊機制入口網站。您可能需要登入。
- 按一下 settings_ethernet「Service accounts」。
- 按一下 add「連結服務帳戶」。
- 將「Email address」(電子郵件地址) 設為您建立的服務帳戶的電子郵件地址。
- 按一下「連結服務帳戶」,即可使用服務帳戶和零接觸註冊帳戶。
步驟 3:準備專案
- 在 Visual Studio 中建立新的 .NET Core C# 主控台應用程式專案。
- 開啟套件管理員,選取套件來源 nuget.org,然後新增下列套件:
Google.Apis.AndroidProvisioningPartner.v1
Google.Apis.Auth
詳情請參閱 Microsoft 文件「安裝及使用套件」。
步驟 4:設定範例
- 將
service_account_key.json
(在步驟 1 中下載) 拖曳至 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「Service Accounts」。
- 從「您的經銷商 ID」行複製合作夥伴 ID 編號。
步驟 5:執行範例
如要建構及執行範例,請按一下 Visual Studio 工具列中的 play_arrow
「Start」。
疑難排解
請告訴我們快速入門課程有哪些錯誤,我們會盡力修正。如要瞭解零接觸技術如何使用服務帳戶授權 API 呼叫,請參閱「授權」一文。
瞭解詳情
除非另有註明,否則本頁面中的內容是採用創用 CC 姓名標示 4.0 授權,程式碼範例則為阿帕契 2.0 授權。詳情請參閱《Google Developers 網站政策》。Java 是 Oracle 和/或其關聯企業的註冊商標。
上次更新時間:2025-08-29 (世界標準時間)。
[null,null,["上次更新時間: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)"]]