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

このクイックスタート ガイドの手順を実施すると、約 10 分で ゼロタッチ登録へのリクエストを行うシンプルな .NET C# コンソール アプリ 提供します。

前提条件

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

  • Google アカウント(ゼロタッチ登録のお客様のメンバー) あります。詳細については、Customer 。
  • Visual Studio 2013 以降。
  • インターネット アクセスとウェブブラウザ。

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

  1. こちらの ウィザードを使用して、Google Developers Console でプロジェクトを作成または選択し、 API が自動的に有効になります。[続行] をクリックし、[認証情報に進む] をクリックします。
  2. [認証情報の作成] で [キャンセル] をクリックします。
  3. ページ上部の [OAuth 同意画面] タブを選択します。以下を選択します。 [Email address]: まだ設定されていない場合は [Product name] を入力します。 [保存] ボタンをクリックします。
  4. [認証情報] タブを選択し、[認証情報を作成] をクリックします。 ボタンをクリックし、[OAuth クライアント ID] を選択します。
  5. アプリケーション タイプとして [その他] を選択し、名前を入力します。 [クイックスタート] を選択し、[Create] をクリックします。 ] ボタンを離します。
  6. [OK] をクリックして [OAuth クライアント] パネルを閉じます。
  7. [ JSON をダウンロード] をクリックします。
  8. ファイルを作業ディレクトリに移動し、名前を client_secret.json に変更します。

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

  1. Visual Studio で新しい .NET Core C# コンソール アプリケーション プロジェクトを作成します。
  2. パッケージ マネージャーを開き、パッケージ ソース nuget.org を選択して、 次のパッケージです。 <ph type="x-smartling-placeholder">
      </ph>
    • Google.Apis.AndroidProvisioningPartner.v1
    • Google.Apis.Auth

詳しくは、Microsoft のドキュメント「 パッケージ

ステップ 3: サンプルをセットアップする

  1. 手順 1 でダウンロードした client_secret.json を Visual Studio にドラッグします ソリューション エクスプローラ。
  2. [client_secret.json] を選択し、[プロパティ] ウィンドウに移動して、 [Copy to output directory] フィールドを [Always copy] に設定します。
  3. Program.cs の内容を次のコードに置き換えます。
using Google.Apis.AndroidProvisioningPartner.v1;
using Google.Apis.AndroidProvisioningPartner.v1.Data;
using Google.Apis.Auth.OAuth2;
using Google.Apis.Services;
using Google.Apis.Util.Store;
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)
        {
            UserCredential credential;

            // Ask the user to authorize the request using their Google Account
            // in their browser.
            using (var stream =
                new FileStream("client_secret.json", FileMode.Open, FileAccess.Read))
            {
                string credPath = System.Environment.GetFolderPath(
                    System.Environment.SpecialFolder.Personal);
                credPath = Path.Combine(credPath, ".credentials/zero-touch.quickstart.json");

                credential = GoogleWebAuthorizationBroker.AuthorizeAsync(
                    GoogleClientSecrets.FromStream(stream).Secrets,
                    Scopes,
                    "user",
                    CancellationToken.None,
                    new FileDataStore(credPath, true)).Result;
                Console.WriteLine("Credential file saved to: " + credPath);
            }

            // 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 ツールバーの [開始] をクリックします。

アプリを初めて実行するときは、アクセスを承認する必要があります。

  1. アプリがデフォルトのブラウザで新しいタブを開こうとします。失敗した場合は、 ブラウザで開きますGoogle アカウントにまだログインしていない場合は、 ログインするように求められます。複数の Google アカウントにログインしている場合は、 承認のためのアカウントを作成します
  2. [Accept] をクリックします。
  3. ブラウザタブを閉じてもアプリは実行を継続します。

メモ

  • Google API クライアント ライブラリは認証データをファイル システムに格納するため、 承認は求められません。
  • アプリの認証データをリセットするには、 ~/.credentials/zero-touch.quickstart.json ファイルを実行し、アプリを再度実行します。
  • このクイックスタートの承認フローは、コマンドライン アプリに最適です。Google Chat で 認証については、をご覧ください。 OAuth 2.0 ウェブ アプリケーション(ASP.NET MVC)を使用する

トラブルシューティング

確認する必要がある一般的な事項は次のとおりです。 クイックスタートの問題点をお聞かせください。修正いたします。

  • お客様のメンバーと同じ Google アカウントで API 呼び出しを承認していることを ゼロタッチ登録の顧客アカウント以下を使用してゼロタッチ登録ポータルにログインしてみてください。 同じ Google アカウントを使ってアクセスをテストします。
  • アカウントが、 ポータル。<ph type="x-smartling-placeholder"></ph>をご覧ください。 顧客アカウント

その他の情報