經銷商適用的 Python 快速入門導覽課程

請按照這份快速入門指南中的步驟進行,大約 10 分鐘後 簡單的 Python 指令列應用程式,向零接觸 。

必要條件

如要執行本快速入門導覽課程,您需要:

  • 擁有零接觸註冊機制經銷商的 Google 帳戶 讓他們使用服務帳戶如果您尚未加入,請按照 請參閱經銷商入口網站指南
  • Python 2.6 以上版本。
  • pip 套件管理 如果偏好在終端機視窗中工作 可使用 Google Cloud CLI gcloud 指令列工具
  • 連上網際網路和網路瀏覽器。

步驟 1:啟用零接觸註冊機制 API

  1. 請使用此 精靈在 Google Developers Console 中建立或選取專案,並且 系統會自動啟用 API依序點選「繼續」和「前往憑證」
  2. 將「您要存取哪些資料?」設為「應用程式資料」
  3. 按一下「繼續」,系統會提示您建立服務 讓他們使用服務帳戶
  4. 為「服務帳戶名稱」設定描述性的名稱。
  5. 記下「服務帳戶 ID」 (形式為電子郵件地址), 以便稍後使用
  6. 角色設為服務帳戶 >服務帳戶使用者
  7. 按一下「Done」(完成),即完成建立服務帳戶。
  8. 點選您建立的服務帳戶的電子郵件地址。
  9. 按一下「鍵」。
  10. 依序按一下「新增金鑰」和「建立新的金鑰」。
  11. 在「金鑰類型」部分選取「JSON」。
  12. 按一下「建立」,私密金鑰就會下載到您的電腦。
  13. 按一下「關閉」。
  14. 將檔案移至工作目錄,並重新命名為 service_account_key.json
  1. 開啟零接觸註冊機制入口網站。您可能需要登入。
  2. 按一下 服務 帳戶
  3. 按一下「連結服務帳戶」
  4. 將「Email address」(電子郵件地址) 設為您建立的服務帳戶的電子郵件地址。
  5. 按一下「連結服務帳戶」,即可透過零接觸使用服務帳戶 註冊帳戶。

步驟 3:安裝 Google 用戶端程式庫

執行下列指令,使用 pip 安裝程式庫:

pip install --upgrade google-api-python-client

查看程式庫的安裝 頁面,查看不同的安裝內容 只要設定成「自動重新啟動」 和「在主機維護期間」選項即可

步驟 4:設定範例

在工作目錄中建立名為 quickstart.py 的檔案。複製至 下列程式碼並儲存檔案。插入自己的經銷商合作夥伴 ID 做為 PARTNER_ID 的值 (應用程式第一行在 匯入)。

#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""Zero-touch enrollment reseller quickstart.

This script forms the quickstart introduction to the zero-touch enrollemnt
reseller API. To learn more, visit https://developer.google.com/zero-touch
"""

from apiclient.discovery import build
from httplib2 import Http
from oauth2client.service_account import ServiceAccountCredentials

# TODO: replace this with your partner reseller ID.
PARTNER_ID = '11036885';

# A single auth scope is used for the zero-touch enrollment customer API.
SCOPES = ['https://www.googleapis.com/auth/androidworkprovisioning']
SERVICE_ACCOUNT_KEY_FILE = 'service_account_key.json'

def get_credential():
  """Creates a Credential object with the correct OAuth2 authorization.

  Creates a Credential object with the correct OAuth2 authorization
  for the service account that calls the reseller API. The service
  endpoint calls this method when setting up a new service instance.

  Returns:
    Credential, the user's credential.
  """
  credential = ServiceAccountCredentials.from_json_keyfile_name(
      SERVICE_ACCOUNT_KEY_FILE, scopes=SCOPES)
  return credential


def get_service():
  """Creates a service endpoint for the zero-touch enrollment reseller 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(Http())
  service = build('androiddeviceprovisioning', 'v1', http=http_auth)
  return service


def main():
  """Runs the zero-touch enrollment quickstart app.
  """
  # Create a zero-touch enrollment API service endpoint.
  service = get_service()

  # Send an API request to list all our customers.
  response = service.partners().customers().list(partnerId=PARTNER_ID).execute()

  # Print out the details of each customer.
  if 'customers' in response:
    for customer in response['customers']:
      print 'Name:{0}  ID:{1}'.format(
          customer['companyName'], customer['companyId'])
  else:
    print 'No customers found'


if __name__ == '__main__':
  main()

合作夥伴 ID

API 呼叫通常需要您的經銷商合作夥伴 ID 做為引數。如要找出 從零接觸註冊機制入口網站取得合作夥伴 ID,請按照下列步驟操作:

  1. 開啟入口網站,您可能需要登入。
  2. 按一下 服務 帳戶
  3. 從「您的經銷商 ID」行複製合作夥伴 ID 編號。

步驟 5:執行範例

利用作業系統的相關說明,執行檔案中的指令碼。UNIX 和 Mac ,在終端機中執行下列指令:

python quickstart.py

列印 API 回應

如要更輕鬆地在試用 API 時檢查回應,請格式化 JSON 回應資料。下列程式碼片段示範如何在 Python 中使用 JSON 模組:

from json import dumps

# ...

results = provisioning.partners().devices().claimAsync(partnerId=MY_PARTNER_ID,
 body={'claims':new_claims}).execute()
# Print formatted JSON response
print dumps(results, indent=4, sort_keys=True)

疑難排解

透過快速入門導覽課程告訴我們您遇到的問題,我們會盡力協助您 修復問題。如想瞭解零接觸機制如何使用服務帳戶授權 API 呼叫,請參閱 授權

瞭解詳情