Hướng dẫn nhanh về miền .NET cho khách hàng

Làm theo các bước trong hướng dẫn nhanh này và trong khoảng 10 phút, bạn sẽ có một ứng dụng bảng điều khiển .NET C# đơn giản giúp thực hiện các yêu cầu đối với API khách hàng thiết lập tự động bằng tài khoản dịch vụ.

Điều kiện tiên quyết

Để chạy tính năng khởi động nhanh này, bạn cần:

  • Một tài khoản dịch vụ được liên kết với tài khoản khách hàng thiết lập tự động. Xem phần Bắt đầu.
  • Visual Studio 2013 trở lên.
  • Truy cập Internet và trình duyệt web.

Bước 1: Bật API thiết lập tự động

  1. Sử dụng trình hướng dẫn này để tạo hoặc chọn một dự án trong Google Developers Console và tự động bật API. Nhấp vào Tiếp tục, sau đó nhấp vào Chuyển đến thông tin đăng nhập.
  2. Đặt Bạn sẽ truy cập dữ liệu nào? thành Dữ liệu ứng dụng.
  3. Nhấp vào Tiếp theo. Bạn sẽ được nhắc tạo một tài khoản dịch vụ.
  4. Đặt tên mô tả cho Tên tài khoản dịch vụ.
  5. Hãy lưu ý đến Mã tài khoản dịch vụ (có vẻ giống như địa chỉ email) vì bạn sẽ sử dụng thông tin này sau.
  6. Đặt Vai trò thành Tài khoản dịch vụ > Người dùng tài khoản dịch vụ.
  7. Nhấp vào Xong để hoàn tất việc tạo tài khoản dịch vụ.
  8. Nhấp vào địa chỉ email cho tài khoản dịch vụ mà bạn đã tạo.
  9. Nhấp vào **Khóa**.
  10. Nhấp vào **Thêm khoá**, sau đó nhấp vào **Tạo khoá mới**.
  11. Đối với **Loại khóa**, hãy chọn **JSON**.
  12. Nhấp vào Tạo và khóa cá nhân sẽ được tải xuống máy tính của bạn.
  13. Nhấp vào **Đóng**.
  14. Di chuyển tệp vào thư mục đang làm việc và đổi tên service_account_key.json.

Bước 2: Chuẩn bị dự án

  1. Tạo một dự án Ứng dụng bảng điều khiển .NET Core C# mới trong Visual Studio.
  2. Mở Trình quản lý gói, chọn nguồn gói nuget.org rồi thêm các gói sau:
    • Google.Apis.AndroidProvisioningPartner.v1
    • Google.Apis.Auth

Để tìm hiểu thêm, hãy đọc tài liệu Microsoft Cài đặt và sử dụng gói.

Bước 3: Thiết lập mẫu

  1. Kéo service_account_key.json mà bạn đã tải xuống khi tạo tài khoản dịch vụ vào Trình khám phá giải pháp Visual Studio.
  2. Chọn service_account_key.json, sau đó chuyển đến cửa sổ Properties (Thuộc tính) rồi đặt trường Copy to output directory (Sao chép vào thư mục đầu ra) thành Always copy (Luôn sao chép).
  3. Thay thế nội dung của Program.cs bằng mã sau:
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;
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)
        {
            GoogleCredential credential;

            // Authenticate using the service account key
            credential = GoogleCredential.FromFile("service_account_key.json")
                .CreateScoped(Scopes);

            // 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);
            }

        }
    }
}

Bước 4: Chạy mẫu

Để tạo và chạy mẫu, hãy nhấp vào Start (Bắt đầu) trên thanh công cụ Visual Studio.

Ghi chú

Tìm hiểu thêm