面向转销商的 Python 快速入门

按照本快速入门指南中的步骤操作,在大约 10 分钟内 简单的 Python 命令行应用程序,用于向零触摸 enrollment reseller API。

前提条件

如需运行本快速入门,您需要:

  • 一个 Google 账号(属于您的零触摸注册转销商的成员) 。如果您尚未完成初始配置,请按照使用入门中的步骤操作 转销商门户指南
  • Python 2.6 或更高版本。
  • pip 软件包管理 工具。
  • 能够访问互联网和网络浏览器。

第 1 步:启用零触摸注册 API

  1. 使用此 向导,以在 Google Developers Console 中创建或选择项目。 系统会自动启用该 API。点击继续,然后点击前往凭据页面
  2. 您要访问哪些数据?设置为应用数据
  3. 点击下一步。系统应该会提示您创建 Service 。
  4. 服务账号名称指定一个描述性名称。
  5. 记下服务账号 ID(看起来像是电子邮件地址),因为您将 稍后使用。
  6. 角色设置为服务账号 >Service Account User
  7. 点击完成以完成服务账号的创建过程。
  8. 点击您创建的服务账号的电子邮件地址。
  9. 点击 **Keys**。
  10. 点击 **Add key**,然后点击 **Create new key**。
  11. 对于 **密钥类型**,选择 **JSON**。
  12. 点击创建,私钥便会下载到您的计算机。
  13. 点击 **Close**。
  14. 将文件移动到工作目录,并将其重命名为 service_account_key.json
  1. 打开零触摸注册门户。您可能需要登录。
  2. 点击 服务 账号
  3. 点击 关联服务账号
  4. 电子邮件地址设置为您创建的服务账号的地址。
  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 格式 响应数据。以下代码段展示了如何使用 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 调用授权,请阅读 授权

了解详情