使用 Python 预配服务 SDK 将 TPM 设备注册到 IoT 中心设备预配服务Enroll TPM device to IoT Hub Device Provisioning Service using Python provisioning service SDK

以下步骤演示了如何借助示例 Python 应用程序,使用 Python 预配服务 SDK 在 Azure IoT 中心设备预配服务中以编程方式为 TPM 设备创建单个注册。These steps show how to programmatically create an individual enrollment for a TPM device in the Azure IoT Hub Device Provisioning Service, using the Python Provisioning Service SDK with the help of a sample Python application. 虽然 Python 服务 SDK 在 Windows 和 Linux 计算机上均适用,但本文使用 Windows 开发计算机来演示注册过程。Although the Python Service SDK works on both Windows and Linux machines, this article uses a Windows development machine to walk through the enrollment process.

在继续操作之前,请确保通过 Azure 门户设置 IoT 中心设备预配服务Make sure to set up IoT Hub Device Provisioning Service with the Azure portal before you proceed.

准备环境Prepare the environment

  1. 下载并安装 Python 2.x 或 3.xDownload and install Python 2.x or 3.x. 请确保根据安装程序的要求,使用 32 位或 64 位安装。Make sure to use the 32-bit or 64-bit installation as required by your setup. 在安装过程中出现提示时,请确保将 Python 添加到特定于平台的环境变量中。When prompted during the installation, make sure to add Python to your platform-specific environment variables.

  2. 选择以下选项之一:Choose one of the following options:

  3. 需要适用于设备的认可密钥。You need the endorsement key for your device. 如果已按照创建和预配模拟的设备快速入门教程创建模拟的 TPM 设备,请使用为该设备创建的密钥。If you have followed the Create and provision a simulated device quickstart to create a simulated TPM device, use the key created for that device. 否则,可以使用 SDK 随附的以下认可密钥:Otherwise, you can use the following endorsement key supplied with the SDK:

    AToAAQALAAMAsgAgg3GXZ0SEs/gakMyNRqXXJP1S124GUgtk8qHaGzMUaaoABgCAAEMAEAgAAAAAAAEAtW6MOyCu/Nih47atIIoZtlYkhLeCTiSrtRN3q6hqgOllA979No4BOcDWF90OyzJvjQknMfXS/Dx/IJIBnORgCg1YX/j4EEtO7Ase29Xd63HjvG8M94+u2XINu79rkTxeueqW7gPeRZQPnl1xYmqawYcyzJS6GKWKdoIdS+UWu6bJr58V3xwvOQI4NibXKD7htvz07jLItWTFhsWnTdZbJ7PnmfCa2vbRH/9pZIow+CcAL9mNTNNN4FdzYwapNVO+6SY/W4XU0Q+dLMCKYarqVNH5GzAWDfKT8nKzg69yQejJM8oeUWag/8odWOfbszA+iFjw3wVNrA5n8grUieRkPQ==
    

修改 Python 示例代码Modify the Python sample code

此部分演示如何向示例代码添加 TPM 设备的预配详细信息。This section shows how to add the provisioning details of your TPM device to the sample code.

  1. 使用文本编辑器,新建一个 TpmEnrollment.py 文件。Using a text editor, create a new TpmEnrollment.py file.

  2. TpmEnrollment.py 文件的开头添加以下 import 语句和变量:Add the following import statements and variables at the start of the TpmEnrollment.py file. 然后,将 dpsConnectionString 替换为你的连接字符串,该字符串位于 Azure 门户设备预配服务的“共享访问策略”下。Then replace dpsConnectionString with your connection string found under Shared access policies in your Device Provisioning Service on the Azure portal. endorsementKey 替换为此前在准备环境中记下的值。Replace endorsementKey with the value noted previously in Prepare the environment. 最后,创建唯一的 registrationid,确保其只包含小写字母数字和连字符。Finally, create a unique registrationid and be sure that it only consists of lower-case alphanumerics and hyphens.

    from provisioningserviceclient import ProvisioningServiceClient
    from provisioningserviceclient.models import IndividualEnrollment, AttestationMechanism
    
    CONNECTION_STRING = "{dpsConnectionString}"
    
    ENDORSEMENT_KEY = "{endorsementKey}"
    
    REGISTRATION_ID = "{registrationid}"
    
  3. 添加以下函数和函数调用,以便执行组注册创建操作:Add the following function and function call to implement the group enrollment creation:

    def main():
        print ( "Starting individual enrollment..." )
    
        psc = ProvisioningServiceClient.create_from_connection_string(CONNECTION_STRING)
    
        att = AttestationMechanism.create_with_tpm(ENDORSEMENT_KEY)
        ie = IndividualEnrollment.create(REGISTRATION_ID, att)
    
        ie = psc.create_or_update(ie)
    
        print ( "Individual enrollment successful." )
    
    if __name__ == '__main__':
        main()
    
  4. 保存并关闭 TpmEnrollment.py 文件。Save and close the TpmEnrollment.py file.

运行示例 TPM 注册Run the sample TPM enrollment

  1. 打开命令提示符并运行此脚本。Open a command prompt, and run the script.

    python TpmEnrollment.py
    
  2. 观察成功注册后的输出。Observe the output for the successful enrollment.

  3. 在 Azure 门户中导航到预配服务。Navigate to your provisioning service in the Azure portal. 单击“管理注册”。Click Manage enrollments. 请注意,TPM 设备显示在“单个注册”选项卡下,使用的名称 registrationid 是以前创建的。Notice that your TPM device appears under the Individual Enrollments tab, with the name registrationid created earlier.

    验证是否已在门户中成功完成 TPM 注册

清理资源Clean up resources

如果打算学习 Java 服务示例,请勿清除本快速入门中创建的资源。If you plan to explore the Java service sample, do not clean up the resources created in this Quickstart. 如果不打算继续学习,请通过以下步骤删除通过本快速入门创建的所有资源。If you do not plan to continue, use the following steps to delete all resources created by this Quickstart.

  1. 关闭计算机上的 Python 示例输出窗口。Close the Python sample output window on your machine.
  2. 如果已创建模拟的 TPM 设备,请关闭 TPM 模拟器窗口。If you created a simulated TPM device, close the TPM simulator window.
  3. 在 Azure 门户中导航到设备预配服务,单击“管理注册”,然后选择“单个注册”选项卡。选择通过本快速入门创建的注册项的“注册 ID”,然后单击边栏选项卡顶部的“删除”按钮。Navigate to your Device Provisioning service in the Azure portal, click Manage enrollments, and then select the Individual Enrollments tab. Select the Registration ID for the enrollment entry you created using this Quickstart, and click the Delete button at the top of the blade.

后续步骤Next steps

本快速入门介绍了如何以编程方式为 TPM 设备创建单个注册项,以及如何选择性地在计算机上创建 TPM 模拟设备,并使用 Azure IoT 中心设备预配服务将其预配到 IoT 中心。In this Quickstart, you’ve programmatically created an individual enrollment entry for a TPM device, and, optionally, created a TPM simulated device on your machine and provisioned it to your IoT hub using the Azure IoT Hub Device Provisioning Service. 若要深入了解设备预配,请继续学习本教程有关如何在 Azure 门户中进行设备预配服务设置的内容。To learn about device provisioning in depth, continue to the tutorial for the Device Provisioning Service setup in the Azure portal.