设置 Devices API
本页面介绍如何设置 Cloud Identity Devices API。您可以使用 Devices API 以编程方式代表管理员预配资源,例如管理 Google 群组。
启用 API 并设置凭据
- 登录您的 Google Cloud 账号。如果您是 Google Cloud新手,请 创建一个账号来评估我们的产品在实际场景中的表现。新客户还可获享 $300 赠金,用于运行、测试和部署工作负载。
-
In the Google Cloud console, on the project selector page, select or create a Google Cloud project.
Roles required to select or create a project
- Select a project: Selecting a project doesn't require a specific IAM role—you can select any project that you've been granted a role on.
-
Create a project: To create a project, you need the Project Creator role
(
roles/resourcemanager.projectCreator), which contains theresourcemanager.projects.createpermission. Learn how to grant roles.
-
Verify that billing is enabled for your Google Cloud project.
Enable the Cloud Identity API.
Roles required to enable APIs
To enable APIs, you need the Service Usage Admin IAM role (
roles/serviceusage.serviceUsageAdmin), which contains theserviceusage.services.enablepermission. Learn how to grant roles.-
In the Google Cloud console, on the project selector page, select or create a Google Cloud project.
Roles required to select or create a project
- Select a project: Selecting a project doesn't require a specific IAM role—you can select any project that you've been granted a role on.
-
Create a project: To create a project, you need the Project Creator role
(
roles/resourcemanager.projectCreator), which contains theresourcemanager.projects.createpermission. Learn how to grant roles.
-
Verify that billing is enabled for your Google Cloud project.
Enable the Cloud Identity API.
Roles required to enable APIs
To enable APIs, you need the Service Usage Admin IAM role (
roles/serviceusage.serviceUsageAdmin), which contains theserviceusage.services.enablepermission. Learn how to grant roles.
使用具有全网域授权的服务账号设置 API 访问权限
本部分介绍如何创建和使用服务账号来访问 Google Workspace 资源。不支持使用服务账号直接向 Devices API 进行身份验证,因此您必须使用此方法。
创建服务账号并将其配置为支持全网域授权
如需创建服务账号并下载服务账号密钥,请执行以下操作:
如需创建服务账号,请执行以下操作:
在 Google Cloud 控制台中,前往“IAM 服务账号”页面:
点击 创建服务账号。
在服务账号详情下,输入服务账号的名称、ID 和说明,然后点击创建并继续。
可选:在向此服务账号授予对项目的访问权限下,选择要向该服务账号授予的 IAM 角色。
点击继续。
可选:在向用户授予访问此服务账号的权限下,添加允许使用和管理该服务账号的用户或群组。
点击完成。
如需让服务账号使用全网域委托来访问 Devices API,请按照为服务账号设置全网域委托中的说明操作。
如需创建并下载服务账号密钥,请执行以下操作。
- 点击您创建的服务账号的电子邮件地址。
- 点击密钥标签页。
- 在添加密钥下拉列表中,选择创建新密钥。
点击创建。
系统会生成一个包含新公钥/私钥对的 JSON 格式凭据文件,并将其下载到您的计算机。该文件包含密钥的唯一副本。您有责任妥善存储该密钥。如果您丢失了密钥对,则必须生成新的密钥对。
查看日志条目
查看日志条目时,请注意审核日志会将任何服务账号操作显示为由用户发起。这是因为全网域授权的工作原理是允许服务账号模拟管理员用户。
初始化凭据
在代码中初始化凭据时,通过对凭据调用 with_subject() 来指定服务账号要操作的电子邮件地址。例如:
Python
credentials = service_account.Credentials.from_service_account_file(
'SERVICE_ACCOUNT_CREDENTIAL_FILE',
scopes=SCOPES).with_subject(USER
)
替换以下内容:
SERVICE_ACCOUNT_CREDENTIAL_FILE:您在本文档前面部分创建的服务账号密钥文件USER:服务账号模拟的用户
实例化客户端
以下示例展示了如何使用服务账号凭据对客户端进行实例化。
Python
from google.oauth2 import service_account
import googleapiclient.discovery
SCOPES = ['https://www.googleapis.com/auth/cloud-identity.devices']
def create_service():
credentials = service_account.Credentials.from_service_account_file(
'SERVICE_ACCOUNT_CREDENTIAL_FILE',
scopes=SCOPES
)
delegated_credentials = credentials.with_subject('USER')
service_name = 'cloudidentity'
api_version = 'v1'
service = googleapiclient.discovery.build(
service_name,
api_version,
credentials=delegated_credentials)
return service
替换以下内容:
SERVICE_ACCOUNT_CREDENTIAL_FILE:您在本文档前面部分创建的服务账号密钥文件USER:服务账号模拟的用户
现在,您可以开始调用 Devices API。
如需改为以最终用户身份进行身份验证,请将服务账号中的 credential 对象替换为您可在获取 OAuth 2.0 令牌中获得的 credential。
安装 Python 客户端库
如需安装 Python 客户端库,请运行以下命令:
pip install --upgrade google-api-python-client google-auth \
google-auth-oauthlib google-auth-httplib2
如需详细了解如何设置 Python 开发环境,请参阅 Python 开发环境设置指南。