このクイックスタート ガイドの手順に沿って操作すると、サービス アカウントを使用してゼロタッチ登録のカスタマー API にリクエストを送信するシンプルな Python コマンドライン アプリが約 10 分で作成されます。
前提条件
このクイックスタートを実行するには、次のものが必要です。
- ゼロタッチ登録のお客様のアカウントにリンクされているサービス アカウント。詳しくは、 開始されました。
- Python 3.0 以降。
- pip パッケージ管理 ツールです。
- インターネット アクセスとウェブブラウザ。
ステップ 1: ゼロタッチ登録 API を有効にする
- こちらの ウィザードを使用して、Google Developers Console でプロジェクトを作成または選択し、 API が自動的に有効になります。[続行] をクリックし、[認証情報に進む] をクリックします。 。
- [What data will you be access?] を [Application data] に設定します。
- [次へ] をクリックします。サービス アカウントの作成を求めるメッセージが表示されます。
- [サービス アカウント名] にわかりやすい名前を付けます。
- 次の作業を行うため、サービス アカウント 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
ファイルは誰とも共有しないでください。ご注意ください ソースコード リポジトリに含めないようにする必要があります。詳しくは、このモジュールの サービス アカウントのシークレットの処理をご覧ください。