Is there anyway to upload a certificate (in the portal option Security settings | Certificate) into Azure IoTHub using python?
I'm using https://pypi.org/project/azure-iot-hub/
Below the complete python code that automates uploading and verifying a certificate in Azure IoT Hub.
from azure.identity import DefaultAzureCredential
from azure.mgmt.iothub import IotHubClient
SUBSCRIPTION_ID = "86e"
RESOURCE_GROUP = "sampathp"
IOT_HUB_NAME = "sampath122"
CERTIFICATE_NAME = "certificatename1"
CERTIFICATE_PATH = "C://Users//device-cert.pem"
def upload_certificate():
credential = DefaultAzureCredential()
client = IotHubClient(credential, SUBSCRIPTION_ID)
with open(CERTIFICATE_PATH, "r") as cert_file:
certificate_content = cert_file.read()
response = client.certificates.create_or_update(
resource_group_name=RESOURCE_GROUP,
resource_name=IOT_HUB_NAME,
certificate_name=CERTIFICATE_NAME,
certificate_description={
"properties": {
"certificate": certificate_content,
"isVerified": True
}
}
)
print(f"Subject: {response.properties.subject}")
print(f"Expiry: {response.properties.expiry}")
print(f"Thumbprint: {response.properties.thumbprint}")
if __name__ == "__main__":
upload_certificate()