.NET クイックスタート(お客様向け)

このクイックスタート ガイドの手順に沿って操作すると、約 10 分で簡単な .NET C# コンソール アプリが作成され、サービス アカウントを使用してゼロタッチ登録の顧客 API にリクエストを行うことができます。

Prerequisites

このクイックスタートを実行するには、次の準備が必要です。

  • ゼロタッチ登録のお客様アカウントにリンクされたサービス アカウント。スタートガイドをご覧ください。
  • Visual Studio 2013 以降。
  • インターネット アクセスとウェブブラウザ。

ステップ 1: ゼロタッチ登録 API を有効にする

  1. このウィザードを使用して Google Cloud Console でプロジェクトを作成または選択し、API を自動的に有効にします。[続行]、[認証情報に移動] の順にクリックします。
  2. [アクセス対象のデータ] を [アプリケーション データ] に設定します。
  3. [Next] をクリックします。サービス アカウントを作成するよう求められます。
  4. [サービス アカウント名] にわかりやすい名前を付けます。
  5. サービス アカウント ID は後で使用しますので、メモしておきます(メールアドレスのように見えます)。
  6. ロール[サービス アカウント] > [サービス アカウント ユーザー] に設定します。
  7. [完了] をクリックして、サービス アカウントの作成を完了します。
  8. 作成したサービス アカウントのメールアドレスをクリックします。
  9. [**キー**] をクリックします。
  10. [鍵を追加] をクリックしてから、[新しい鍵を作成] をクリックします。
  11. [キーのタイプ] で [JSON] を選択します。
  12. [作成] をクリックして、秘密鍵がパソコンにダウンロードされます。
  13. [**閉じる**] をクリックします。
  14. 作業ディレクトリにファイルを移動し、名前を service_account_key.json に変更します。

ステップ 2: プロジェクトを準備する

  1. Visual Studio で新しい .NET Core C# Console Application プロジェクトを作成します。
  2. パッケージ マネージャーを開き、パッケージ ソース nuget.org を選択して、次のパッケージを追加します。
    • Google.Apis.AndroidProvisioningPartner.v1
    • Google.Apis.Auth

詳細については、Microsoft のドキュメントのパッケージのインストールと使用をご覧ください。

手順 3: サンプルをセットアップする

  1. サービス アカウントの作成時にダウンロードした service_account_key.json を Visual Studio Solution Explorer にドラッグします。
  2. service_account_key.json を選択してから [プロパティ] ウィンドウに移動し、[出力ディレクトリにコピー] フィールドを [常にコピー] に設定します。
  3. Program.cs の内容を次のコードに置き換えます。
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);
            }

        }
    }
}

ステップ 4: サンプルを実行する

サンプルをビルドして実行するには、Visual Studio のツールバーの [ 開始] をクリックします。

メモ

その他の情報