Example of using an OpenAPI
This topic provides a complete example of how to use an OpenAPI to create a directional group.
View the OpenAPI document
Read the API overview and select the OpenAPI for creating a directional group: AddDirectionalGroup. Use this document to understand the data and permissions required to call this API.
Manage user identities
Identity
You can use an Alibaba Cloud account, a Resource Access Management (RAM) user, or a RAM role to call this API. This example uses a RAM user to call the API.
Log on to the Resource Access Management (RAM) console. Create a RAM user that is allowed to make only API calls and record the AccessKey information. For more information, see Create a RAM user.
Authorization
Go to the RAM User List page. In the Actions column for the target RAM user, click Add Permissions.
In the Select Permissions section, enter the keyword
linkcardin the search box and select the AliyunLinkCardFullAccess policy.NoteAliyunLinkCardFullAccess: Grants the permission to manage Internet of Things (IoT) cards (LC).
AliyunLinkCardReadOnlyAccess: Grants the read-only permission to manage IoT cards (LC).
For more information about how to create a custom policy, see Authorization information.
Click OK to complete the authorization.
Credentials
An AccessKey is generated by default when you create a RAM user. You can use it directly. You can also go to the details page of the RAM user. On the Authentication Management tab, click Create AccessKey to create a new AccessKey. For more information, see Create an AccessKey.
Call the OpenAPI
This topic uses Python as an example to show you how to call the OpenAPI.
Prepare the Python environment
Download and install Python.
To download Python 3, visit the official Python 3 website.
Check the Python version.
Linux and macOS
Open the terminal and run the
python --versioncommand.Windows operating system
In the Python installation path, double-click python.exe.
Open the Command Prompt window.
Press the Win+R keyboard shortcut to open the Run dialog box. Enter python and click OK.
Configure environment variables
To avoid the security risks of hard coding an AccessKey into your business code, this example uses environment variables to manage the AccessKey.
Linux and macOS
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 operating system
Create a new environment variable file. Add the ALIBABA_CLOUD_ACCESS_KEY_ID and ALIBABA_CLOUD_ACCESS_KEY_SECRET environment variables and set their values to your AccessKey ID and AccessKey secret. Then, restart Windows.
Install dependencies
pip install alibabacloud_credentials
pip install alibabacloud_linkcard20210520==1.0.10
pip install alibabacloud_tea_consoleDownload the sample code
In the OpenAPI Portal, go to the debug page for the AddDirectionalGroup API.
Enter a name for the directional group and click Send Request.
After the call is successful, click the SDK Example tab. Select Python and click Download Complete Project to download the software development kit (SDK) example for Python.
Unzip the Python sample code package on your local machine and navigate to the
alibabacloud_sampledirectory.
Run the code
Run the following command:
python sample.pyThe following output is returned:
{
"headers": {
"date": "Thu, 10 Aug 2023 09:32:36 GMT",
"content-type": "application/json;charset=utf-8",
"content-length": "108",
"connection": "keep-alive",
"keep-alive": "timeout=25",
"access-control-allow-origin": "*",
"access-control-expose-headers": "*",
"x-acs-request-id": "C378813C-FE74-5649-A36D-DE8F0******",
"x-acs-trace-id": "e8251c94f4931e03480ebd170******",
"etag": "1AoDYJ5m6Ox/nFKr4sa+M5g8"
},
"statusCode": 200,
"body": {
"Data": 10000004493,
"LocalizedMessage": "",
"RequestId": "C378813C-FE74-5649-A36D-DE8F08******",
"Success": true
}
}