参考ページ
az iot hub generate-sas-token
az iot hub generate-sas-token -n iot-aa
実行結果
$ 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"
}
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("*** 開始 ***\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("*** 終了 ***\n")
# ------------------------------------------------------------------
generate_sas_token.py はこちら
Azure IoT Hub に paho で publish
.env
IOTHUB='iot-aa'
DEVICE='pansy'
MAIN_KEY='2eQ2wF5OjZLP7tq6PG123456789RTfAnMJbZ8wtJPu4='