このクイックスタート ガイドの手順を実施してください。約 10 分で、サービス アカウントを使用してゼロタッチ登録の顧客 API にリクエストを送信する、シンプルな Python コマンドライン アプリが作成されます。
Prerequisites
このクイックスタートを実行するには、次の準備が必要です。
- ゼロタッチ登録のお客様アカウントにリンクされたサービス アカウント。スタートガイドをご覧ください。
- Python 3.0 以降。
- pip パッケージ管理ツール
- インターネット アクセスとウェブブラウザ。
ステップ 1: ゼロタッチ登録 API を有効にする
- このウィザードを使用して Google Cloud Console でプロジェクトを作成または選択し、API を自動的に有効にします。[続行]、[認証情報に移動] の順にクリックします。
- [アクセス対象のデータ] を [アプリケーション データ] に設定します。
- [Next] をクリックします。サービス アカウントを作成するよう求められます。
- [サービス アカウント名] にわかりやすい名前を付けます。
- サービス アカウント ID は後で使用しますので、メモしておきます(メールアドレスのように見えます)。
- ロールを [サービス アカウント] > [サービス アカウント ユーザー] に設定します。
- [完了] をクリックして、サービス アカウントの作成を完了します。
- 作成したサービス アカウントのメールアドレスをクリックします。
- [**キー**] をクリックします。
- [鍵を追加] をクリックしてから、[新しい鍵を作成] をクリックします。
- [キーのタイプ] で [JSON] を選択します。
- [作成] をクリックして、秘密鍵がパソコンにダウンロードされます。
- [**閉じる**] をクリックします。
- 作業ディレクトリにファイルを移動し、名前を
service_account_key.json
に変更します。
ステップ 2: Google クライアント ライブラリをインストールする
pip を使用して、次のコマンドを実行してライブラリをインストールします。
pip install --upgrade google-api-python-client oauth2client
さまざまなインストール オプションについては、ライブラリのインストール ページをご覧ください。
手順 3: サンプルをセットアップする
作業ディレクトリに quickstart.py
という名前のファイルを作成します。次のコードをコピーしてファイルを保存します。
#!/usr/bin/env python # -*- coding: utf-8 -*- """Zero-touch enrollment quickstart sample. This script forms the quickstart introduction to the zero-touch enrollemnt customer API. To learn more, visit https://developer.google.com/zero-touch """ import sys from apiclient import discovery import httplib2 from oauth2client.service_account import ServiceAccountCredentials # A single auth scope is used for the zero-touch enrollment customer API. SCOPES = ['https://www.googleapis.com/auth/androidworkzerotouchemm'] SERVICE_ACCOUNT_KEY_FILE = 'service_account_key.json' def get_credential(): """Creates a Credential object with the correct OAuth2 authorization. Uses the service account key stored in SERVICE_ACCOUNT_KEY_FILE. Returns: Credentials, the user's credential. """ credential = ServiceAccountCredentials.from_json_keyfile_name( SERVICE_ACCOUNT_KEY_FILE, SCOPES) if not credential or credential.invalid: print('Unable to authenticate using service account key.') sys.exit() return credential def get_service(): """Creates a service endpoint for the zero-touch enrollment API. Builds and returns an authorized API client service for v1 of the API. Use the service endpoint to call the API methods. Returns: A service Resource object with methods for interacting with the service. """ http_auth = get_credential().authorize(httplib2.Http()) return discovery.build('androiddeviceprovisioning', 'v1', http=http_auth) def main(): """Runs the zero-touch enrollment quickstart app. """ # Create a zero-touch enrollment API service endpoint. service = get_service() # Get the customer's account. Because a customer might have more # than one, limit the results to the first account found. response = service.customers().list(pageSize=1).execute() if 'customers' not in response: # No accounts found for the user. Confirm the Google Account # that authorizes the request can access the zero-touch portal. print('No zero-touch enrollment account found.') sys.exit() customer_account = response['customers'][0]['name'] # Send an API request to list all the DPCs available using the customer # account. results = service.customers().dpcs().list(parent=customer_account).execute() # Print out the details of each DPC. for dpc in results['dpcs']: # Some DPCs may not have a name, so replace with a marker. if 'dpcName' in dpc: dpcName = dpc['dpcName'] else: dpcName = "-" print('Name:{0} APK:{1}'.format(dpcName, dpc['packageName'])) if __name__ == '__main__': main()
ステップ 4: サービス アカウント キーを追加する
サービス アカウントの作成時にダウンロードした service_account_key.json
を作業ディレクトリにコピーします。
ステップ 5: サンプルを実行する
オペレーティング システムのヘルプを使用して、ファイル内のスクリプトを実行します。UNIX と Mac のパソコンの場合は、ターミナルで次のコマンドを実行します。
python quickstart.py
メモ
service_account_key.json
ファイルは誰とも共有しないでください。ソースコード リポジトリに含めないように注意してください。詳細については、サービス アカウントのシークレットの処理をご覧ください。