按照此快速入门指南中的步骤操作,您将在大约 10 分钟后获得一个简单的 Python 命令行应用,该应用会向零触摸注册转销商 API 发出请求。
前提条件
如需运行本快速入门,您需要:
- Google 帐号,即您的零触摸注册转销商帐号的成员。如果您尚未进行初始配置,请按照转销商门户指南中使用入门中的步骤操作。
- Python 2.6 或更高版本。
- pip 软件包管理工具。
- 能够访问互联网和网络浏览器。
第 1 步:启用零触摸注册 API
- 使用此向导在 Google Developers Console 中创建或选择项目,并自动启用 API。点击继续,然后点击转到凭据。
- 将您要访问哪些数据?设为应用数据。
- 点击下一步。系统应该会提示您创建服务帐号。
- 为服务帐号名称指定一个描述性名称。
- 记下服务帐号 ID(看起来像是电子邮件地址),因为您稍后会用到它。
- 将角色设置为 Service Accounts > Service Account User。
- 点击完成以完成服务帐号的创建过程。
- 点击您创建的服务帐号的电子邮件地址。
- 点击 **Keys**。
- 点击 **添加密钥**,然后点击 **创建新密钥**。
- 对于 **密钥类型**,选择 **JSON**。
- 点击创建,然后将私钥下载到您的计算机。
- 点击 **Close**。
- 将该文件移动到工作目录,并将其重命名为
service_account_key.json
。
第 2 步:关联服务帐号
- 打开零触摸注册门户。您可能需要登录。
- 点击 服务帐号。
- 点击 关联服务帐号。
- 将电子邮件地址设置为您创建的服务帐号的地址。
- 点击关联服务帐号,将该服务帐号与您的零触摸注册帐号结合使用。
第 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,请按以下步骤操作:
- 打开门户网站。您可能需要登录。
- 点击 服务帐号。
- 复制您的转销商 ID 行中的合作伙伴 ID 编号。
第 5 步:运行示例
在操作系统的帮助下,运行文件中的脚本。在 UNIX 和 Mac 计算机上,在终端中运行以下命令:
python quickstart.py
输出 API 响应
为了在试用 API 时更轻松地检查响应,请设置 JSON 响应数据的格式。以下代码段展示了如何在 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 调用进行授权,请参阅授权。