def patch_rsa256_auth( service_account_json, project_id, cloud_region, registry_id, device_id, public_key_file, ): print("Patch device with RSA256 certificate") client = iot_v1.DeviceManagerClient() device_path = client.device_path(project_id, cloud_region, registry_id, device_id) public_key_bytes = "" with io.open(public_key_file) as f: public_key_bytes = f.read() key = iot_v1.PublicKeyCredential( format=iot_v1.PublicKeyFormat.RSA_X509_PEM, key=public_key_bytes) cred = iot_v1.DeviceCredential(public_key=key) device = client.get_device(request={"name": device_path}) device.id = b"" device.num_id = 0 device.credentials.append(cred) mask = gp_field_mask.FieldMask() mask.paths.append("credentials") return client.update_device(request={ "device": device, "update_mask": mask })
def patch_rsa256_auth( service_account_json, project_id, cloud_region, registry_id, device_id, public_key_file, ): """Patch the device to add an RSA256 public key to the device.""" # [START iot_patch_rsa] # project_id = 'YOUR_PROJECT_ID' # cloud_region = 'us-central1' # registry_id = 'your-registry-id' # device_id = 'your-device-id' # public_key_file = 'path/to/certificate.pem' print("Patch device with RSA256 certificate") client = iot_v1.DeviceManagerClient() device_path = client.device_path(project_id, cloud_region, registry_id, device_id) public_key_bytes = "" with io.open(public_key_file) as f: public_key_bytes = f.read() key = iot_v1.PublicKeyCredential( format=iot_v1.PublicKeyFormat.RSA_X509_PEM, key=public_key_bytes) cred = iot_v1.DeviceCredential(public_key=key) device = client.get_device(request={"name": device_path}) device.id = b"" device.num_id = 0 device.credentials.append(cred) mask = gp_field_mask.FieldMask() mask.paths.append("credentials") return client.update_device(request={ "device": device, "update_mask": mask })