A complete example of publishing a message to a device over a custom topic by using an OpenAPI operation.
View the OpenAPI document
Check the API list to find the Pub operation, which publishes messages to a device over a custom topic. Review its documentation to learn about the required data and permissions.
Create a RAM user and grant permissions
Identity
You can call this operation by using an Alibaba Cloud account, a Resource Access Management (RAM) user, or a RAM role. For more information about the differences between these identities, see Identities, credentials, and authorization. This example uses a RAM user.
Log on to the Resource Access Management (RAM) console, create a RAM user for API calls only, and save the AccessKey pair. For more information, see Create a RAM user.
Authorization
-
Go to the RAM User List page. In the Actions column of the target RAM user, click Add Permissions.
-
In the Select Policy section, enter the keyword
IoTin the search box and select the AliyunIOTFullAccess policy.Note-
The system policies for IoT Platform are as follows:
-
AliyunIOTFullAccess: Grants full management permissions for Internet of Things (IoT) Platform.
-
AliyunIOTReadOnlyAccess: Grants read-only access permissions for Internet of Things (IoT) Platform.
-
-
For more information about creating a custom policy, see Custom policies.
-
-
Click OK to complete the authorization.
Credential
An AccessKey pair is generated by default when you create a RAM user.
Alternatively, go to the details page of the RAM user. On the Authentication Management tab, click Create AccessKey. For more information, see Create an AccessKey.
Call the OpenAPI
The following example uses Python to call the Pub operation.
Prepare the Python environment
-
Check if Python 3 is installed on your system.
Linux and macOS systems
Open the terminal and run the
python3 --versioncommand.If the system returns the Python version number, Python 3 is installed. Otherwise, proceed to the next step to install Python 3.
Windows system
In the Python installation path, double-click python.exe to open a command prompt window. You can also use the Win+R keyboard shortcut to open the Run dialog box, enter python, and click OK.
If the system returns a response similar to the following and enters the Python interactive environment, Python is installed.
Python 3.9.6 (tags/v3.9.6:db3ff76, Jun 28 2021, 15:26:21) [MSC v.1929 64 bit (AMD64)] on win32 Type "help", "copyright", "credits" or "license" for more information. >>> -
Download and install Python.
To download Python 3, see Python 3.
Linux
-
Download the Python package.
wget https://www.python.org/ftp/python/3.9.6/Python-3.9.6.tgz -
Decompress the Python package.
tar -zxvf Python-3.9.6.tgz -
Switch to the decompressed folder and specify the installation path.
cd Python-3.9.6 sudo ./configure --prefix=/usr/bin/python3 -
Compile and install Python.
make make install -
Configure the environment variable for Python 3.
For example, on an Ubuntu Linux system, you can run the following commands.
-
Open the
.bashrcfile and add the following to the end of the file.nano ~/.bashrc -
Add the following content to the end of the file, then save and close the file.
export PATH=$PATH:/usr/bin/python3 -
Run the following command to update the environment variable.
source ~/.bashrc -
Run the following command to view the environment variable and confirm that it includes the Python 3 installation path.
echo $PATH
-
macOS
This system comes with Python 2.7. Download and install the latest version of Python 3.x.
-
After you download the installation package, double-click it to open the Python installation wizard.
-
Click Continue, then follow the instructions to configure and install.
-
Configure the environment variable based on the Python 3 installation path.
Windows
-
After the download is complete, double-click the package to open the Python installation wizard. Use the default settings and select Add Python 3.9 to PATH.
-
Click Install Now to start the installation.
-
After the installation is complete, click Close to close the installation window.
-
-
Repeat Step 1 to check the Python 3 version. If the correct version number is displayed, the installation is successful.
Configure environment variables
To avoid hard-coding an AccessKey pair in your code, store the credentials in environment variables.
-
Linux and macOS systems
Replace
<access_key_id>and<access_key_secret>in the following commands with your AccessKey ID and AccessKey secret. Then, run the commands in the terminal.export ALIBABA_CLOUD_ACCESS_KEY_ID=<access_key_id> export ALIBABA_CLOUD_ACCESS_KEY_SECRET=<access_key_secret> -
Windows system
Create the
ALIBABA_CLOUD_ACCESS_KEY_IDandALIBABA_CLOUD_ACCESS_KEY_SECRETenvironment variables and set their values to your AccessKey ID and AccessKey secret. Then, restart your Windows system.
Install dependencies
pip3 install alibabacloud_tea_openapi
pip3 install alibabacloud_iot20180120
pip3 install alibabacloud-tea-console
Download the sample code
-
In the OpenAPI Portal, go to the debugging page for the Pub API.
-
Enter values for the IotInstanceId, ProductKey, TopicFullName, and MessageContent request parameters. You can leave the other parameters empty.
-
Click Submit Request.
-
On the Call Result tab, after the Call Succeeded message appears, click the SDK Sample tab, and then click the Python tab.
-
Click Download Complete Project to download the sample code package.
-
Decompress the sample code package on your local machine and navigate to the alibabacloud_sample folder.
Run the code
Run the following command:
python sample.py
The output is as follows:
{
"headers": {
"date": "Thu, 10 Aug 2023 09:26:52 GMT",
"content-type": "application/json;charset=utf-8",
"content-length": "99",
"connection": "keep-alive",
"keep-alive": "timeout=25",
"access-control-allow-origin": "*",
"access-control-expose-headers": "*",
"x-acs-request-id": "1296FD43-4BD3-5F80-AB2C-1ADF99B02253",
"x-acs-trace-id": "aee72e4c5e***************77fc9d",
"etag": "9umD************cjYJ8og9"
},
"statusCode": 200,
"body": {
"MessageId": 1689000000000004736,
"RequestId": "1296FD43-4BD3-5F80-AB2C-1ADF99B02253",
"Success": true
}
}