Reference page az iot hub generate-sas-token
az iot hub generate-sas-token -n iot-aa
Execution result
$ az iot hub generate-sas-token -n iot-aa
{
"sas": "SharedAccessSignature sr=iot-bb.azure-devices.net&sig=dsEOdeubZMhRFMab7KhTcijzc%2BNaiYjlDcig8lqTrpE%3D&se=1610847623&skn=iothubowner"
}
When writing in Python
main_sas_token.py
#! /usr/bin/python
# -*- coding: utf-8 -*-
#
# main_sas_token.py
#
# Jan/17/2021
# ------------------------------------------------------------------
import sys
import os
from dotenv import load_dotenv
from generate_sas_token import generate_sas_token
# ------------------------------------------------------------------
sys.stderr.write("***start***\n")
dotenv_path = '.env'
load_dotenv(dotenv_path)
IOTHUB = os.environ.get("IOTHUB")
DEVICE = os.environ.get("DEVICE")
MAIN_KEY = os.environ.get("MAIN_KEY")
sys.stderr.write(IOTHUB + "\n")
sys.stderr.write(DEVICE + "\n")
sys.stderr.write(MAIN_KEY + "\n")
#
uri = IOTHUB + ".azure-devices.net/devices/" + DEVICE
#
key = MAIN_KEY
policy_name = None
iexpire = 3600
#
try:
sas_token = generate_sas_token(uri, key, policy_name, iexpire)
except Exception as ee:
sys.stderr.write("*** error *** in generate_sas_token ***\n")
sys.stderr.write(str(ee) + "\n")
#
print(sas_token)
sys.stderr.write("***End***\n")
# ------------------------------------------------------------------
Click here for generate_sas_token.py Publish to Azure IoT Hub with paho (https://qiita.com/ekzemplaro/items/8a2f8ac7bae35a9c91d4)
.env
IOTHUB='iot-aa'
DEVICE='pansy'
MAIN_KEY='2eQ2wF5OjZLP7tq6PG123456789RTfAnMJbZ8wtJPu4='
Recommended Posts