このクイックスタート ガイドの手順に沿って操作すると、約 10 分で簡単な Python コマンドライン アプリが作成され、ゼロタッチ登録の顧客 API にリクエストが送信されます。
Prerequisites
このクイックスタートを実行するには、次の準備が必要です。
- ゼロタッチ登録の顧客アカウントのメンバーである Google アカウント。スタートガイドをご覧ください。
- Python 3.0 以降。
- pip パッケージ管理ツール
- インターネット アクセスとウェブブラウザ。
ステップ 1: ゼロタッチ登録 API を有効にする
- このウィザードを使用して Google Cloud Console でプロジェクトを作成または選択し、API を自動的に有効にします。[続行]、[認証情報に移動] の順にクリックします。
- [認証情報を作成] で [キャンセル] をクリックします。
- ページ上部の [OAuth 同意画面] タブを選択します。[メールアドレス] を選択し、まだ設定されていない場合はプロダクト名を入力して、[保存] ボタンをクリックします。
- [認証情報] タブを選択し、[認証情報を作成] ボタンをクリックして、[OAuth クライアント ID] を選択します。
- アプリケーション タイプとして [その他] を選択し、「クイックスタート」という名前を入力して、[作成] ボタンをクリックします。
- [OK] をクリックして [OAuth クライアント] パネルを閉じます。
- [ JSON をダウンロード] をクリックします。
- 作業ディレクトリにファイルを移動し、名前を
client_secret.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 import tools from oauth2client.client import flow_from_clientsecrets from oauth2client.file import Storage # A single auth scope is used for the zero-touch enrollment customer API. SCOPES = ['https://www.googleapis.com/auth/androidworkzerotouchemm'] CLIENT_SECRET_FILE = 'client_secret.json' USER_CREDENTIAL_FILE = 'user_credential.json' def get_credential(): """Creates a Credential object with the correct OAuth2 authorization. Ask the user to authorize the request using their Google Account in their browser. Because this method stores the cedential in the USER_CREDENTIAL_FILE, the user is typically only asked to the first time they run the script. Returns: Credentials, the user's credential. """ flow = flow_from_clientsecrets(CLIENT_SECRET_FILE, SCOPES) storage = Storage(USER_CREDENTIAL_FILE) credential = storage.get() if not credential or credential.invalid: credential = tools.run_flow(flow, storage) # skipping flags for brevity 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: サンプルを実行する
オペレーティング システムのヘルプを使用して、ファイル内のスクリプトを実行します。UNIX と Mac のパソコンの場合は、ターミナルで次のコマンドを実行します。
python quickstart.py
アプリを初めて実行するときは、アクセスを承認する必要があります。
- アプリがデフォルトのブラウザで新しいタブを開こうとします。失敗した場合は、コンソールから URL をコピーして、ブラウザで開きます。Google アカウントにまだログインしていない場合は、ログインを求められます。複数の Google アカウントにログインしている場合、認可に使用するアカウントを選択するよう求められます。
- [同意する] をクリックします。
- ブラウザタブを閉じます。アプリの実行は続行されます。
メモ
- Google API クライアント ライブラリはファイル システムに認可データを保存するため、後続の起動では認可のプロンプトは表示されません。
- アプリの承認データをリセットするには、
user_credential.json
ファイルを削除してアプリを再度実行します。 - このクイックスタートの承認フローは、コマンドライン アプリに最適です。ウェブアプリに認可を追加する方法については、 ウェブサーバー アプリケーションに OAuth 2.0 を使用するをご覧ください。
トラブルシューティング
確認する一般的な項目は次のとおりです。 クイックスタートで 問題が発生した点を Google にお知らせください。解決に向けて対応いたします。
- ゼロタッチ登録の顧客メンバーと同じ Google アカウントを使用した API 呼び出しを承認していることを確認します。同じ Google アカウントを使用してゼロタッチ登録のポータルにログインし、アクセスをテストします。
- アカウントがポータルの最新の利用規約に同意していることを確認します。 顧客アカウントをご覧ください。