このクイックスタート ガイドの手順により、約 10 分でゼロタッチ登録の顧客 API にリクエストを行うシンプルな .NET C# コンソール アプリが作成されます。
Prerequisites
このクイックスタートを実行するには、次の準備が必要です。
- ゼロタッチ登録の顧客アカウントのメンバーである Google アカウント。顧客アカウントをご覧ください。
- Visual Studio 2013 以降。
- インターネット アクセスとウェブブラウザ。
ステップ 1: ゼロタッチ登録 API を有効にする
- このウィザードを使用して Google Cloud Console でプロジェクトを作成または選択し、API を自動的に有効にします。[続行]、[認証情報に移動] の順にクリックします。
- [認証情報を作成] で [キャンセル] をクリックします。
- ページ上部の [OAuth 同意画面] タブを選択します。[メールアドレス] を選択し、まだ設定されていない場合はプロダクト名を入力して、[保存] ボタンをクリックします。
- [認証情報] タブを選択し、[認証情報を作成] ボタンをクリックして、[OAuth クライアント ID] を選択します。
- アプリケーション タイプとして [その他] を選択し、「クイックスタート」という名前を入力して、[作成] ボタンをクリックします。
- [OK] をクリックして [OAuth クライアント] パネルを閉じます。
- [ JSON をダウンロード] をクリックします。
- 作業ディレクトリにファイルを移動し、名前を
client_secret.json
に変更します。
ステップ 2: プロジェクトを準備する
- Visual Studio で新しい .NET Core C# Console Application プロジェクトを作成します。
- パッケージ マネージャーを開き、パッケージ ソース nuget.org を選択して、次のパッケージを追加します。
Google.Apis.AndroidProvisioningPartner.v1
Google.Apis.Auth
詳細については、Microsoft のドキュメントのパッケージのインストールと使用をご覧ください。
手順 3: サンプルをセットアップする
- ステップ 1 でダウンロードした
client_secret.json
を Visual Studio ソリューション エクスプローラにドラッグします。 - [
client_secret.json
] を選択して [プロパティ] ウィンドウに移動し、[出力ディレクトリにコピー] フィールドを [常にコピー] に設定します。 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 のツールバーの [
開始] をクリックします。アプリを初めて実行するときは、アクセスを承認する必要があります。
- アプリがデフォルトのブラウザで新しいタブを開こうとします。失敗した場合は、コンソールから URL をコピーして、ブラウザで開きます。Google アカウントにまだログインしていない場合は、ログインを求められます。複数の Google アカウントにログインしている場合、認可に使用するアカウントを選択するよう求められます。
- [同意する] をクリックします。
- ブラウザタブを閉じます。アプリの実行は続行されます。
メモ
- Google API クライアント ライブラリはファイル システムに認可データを保存するため、後続の起動では認可のプロンプトは表示されません。
- アプリの承認データをリセットするには、
~/.credentials/zero-touch.quickstart.json
ファイルを削除してアプリを再度実行します。 - このクイックスタートの承認フローは、コマンドライン アプリに最適です。ウェブアプリに承認を追加する方法については、 OAuth 2.0、ウェブ アプリケーション(ASP.NET MVC)の使用をご覧ください。
トラブルシューティング
確認する一般的な項目は次のとおりです。 クイックスタートで 問題が発生した点を Google にお知らせください。解決に向けて対応いたします。
- ゼロタッチ登録の顧客メンバーと同じ Google アカウントを使用した API 呼び出しを承認していることを確認します。同じ Google アカウントを使用してゼロタッチ登録のポータルにログインし、アクセスをテストします。
- アカウントがポータルの最新の利用規約に同意していることを確認します。 顧客アカウントをご覧ください。