How to create a SAS token for Azure IoT Hub

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

How to create a SAS token for Azure IoT Hub
How to create a shortcut command for LINUX
How to create a local repository for Linux OS
How to create a Conda package
How to create a virtual bridge
How to create a Dockerfile (basic)
How to create a config file
How to create a label (mask) for segmentation with labelme (semantic segmentation mask)
How to create a clone from Github
How to create a git clone folder
How to create * .spec files for pyinstaller.
How to create a repository from media
How to create a Python virtual environment (venv)
How to create a function object from a string
How to create a JSON file in Python
How to write a ShellScript Bash for statement
[Note] How to create a Ruby development environment
How to create a Kivy 1-line input box
How to create a multi-platform app with kivy
How to create a Rest Api in Django
[Note] How to create a Mac development environment
What I learned by implementing how to create a Default Box for SSD
Read the Python-Markdown source: How to create a parser
Create a dataset of images to use for learning
How to create a submenu with the [Blender] plugin
How to build a development environment for TensorFlow (1.0.0) (Mac)
How to create a simple TCP server / client script
[Python] How to create a 2D histogram with Matplotlib
How to create a kubernetes pod from python code
Script to easily create a client device environment for AWS IoT (Python v2 version)
Looking for a data disk attached to Azure VM 3000 ri ……
How to manage a README for both github and PyPI
How to set up WSL2 on Windows 10 and create a study environment for Linux commands
How to call a function
How to create a flow mesh around a cylinder with snappyHexMesh
How to define multiple variables in a python for statement
How to make a Python package (written for an intern)
[Python Kivy] How to create a simple pop up window
How to specify Cache-Control for blob storage in Azure Storage in Python
How to substitute a numerical value for a partial match (Note 1)
How to write a test for processing that uses BigQuery
How to hack a terminal
I tried to create a bot for PES event notification
I want to create a Dockerfile for the time being.
Overview of how to create a server socket and how to establish a client socket
How to change the log level of Azure SDK for Python
How to configure CORS settings for Azure storage service in Python
How to create a heatmap with an arbitrary domain in Python
[Introduction to Python] How to use the in operator in a for statement?
[C language] How to create, avoid, and make a zombie process
[For beginners] How to register a library created in Python in PyPI
Spigot (Paper) Introduction to how to make a plug-in for 2020 # 01 (Environment construction)
How to make a unit test Part.1 Design pattern for introduction
How to create a large amount of test data in MySQL? ??
Python vba to create a date string for creating a file name
How to create a CSV dummy file containing Japanese using Faker
How to write a docstring to create a named tuple document with sphinx
How to create a serverless machine learning API with AWS Lambda
How to make a Japanese-English translation
How to write a Python class
How to put a symbolic link