1
3

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

Azure IoT Hub の SAS トークンの作り方

Posted at

参考ページ
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='
1
3
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
3

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?