Python SDK call examples
Call the GetServiceStatus operation by using the CloudSSO SDK for Python to check whether CloudSSO is activated.
Step 1: Review the API documentation
Review the API overview to find the GetServiceStatus operation, which checks the CloudSSO activation status. Note the required parameters and permissions.
Step 2: Create a RAM user and grant permissions
Identity
You can call this operation by using an Alibaba Cloud account, a RAM user, or a RAM role. For more information, see identity.
This example uses a RAM user to call the API operation.
Log on to the RAM console to create a RAM user. For more information, see Create a RAM user.
Credential
Go to the RAM user details page. On the Authentication tab, find the User AccessKeys section and click Create AccessKey. For more information, see Create an AccessKey.
The AccessKey secret of a RAM user is displayed only when it is created and cannot be retrieved later. Store the AccessKey secret in a secure location.
Authorization
-
Go to the Users page on the RAM console. Find the target RAM user and click Assign Access Permissions in the Actions column.
-
Enter the keyword
cloudssoin the search box to find and select the appropriate policy based on the principle of least privilege.-
AliyunCloudSSOFullAccess: grants full access to CloudSSO.
-
AliyunCloudSSOReadOnlyAccess: grants read-only access to CloudSSO.
NoteIf the system policies do not meet your requirements, you can create a custom policy. For more information, see Create a custom policy and Authorization information.
This example uses the AliyunCloudSSOReadOnlyAccess system policy.
-
-
Click OK to complete the authorization.
Step 3: Call the API
This example uses the Python SDK. SDKs for other languages work similarly. For more information, see CloudSSO SDK. For other call methods, see Call methods.
Prepare the Python environment
Download and install Python 3.
After installation, run the python --version command in your terminal to verify the installation.
Configure environment variables
To avoid hard-coding AccessKey pairs in your code, this example stores credentials in environment variables. For more information, see Configure environment variables on Linux, macOS, and Windows systems.
Install dependencies
pip install alibabacloud_credentials
pip install alibabacloud_cloudsso20210515==1.0.2
pip install alibabacloud_tea_console
Download the sample code
-
In OpenAPI Portal, go to the GetServiceStatus debugging page.
-
Enter the request parameters and click Initiate Call to verify that the call succeeds.
This operation requires no request parameters.
-
On the SDK Sample Code tab, select the Python tab. Click Download Project to download the sample code package.
-
Extract the sample code package on your local machine and navigate to the alibabacloud_sample directory.
Run the sample code
Run the following command:
python sample.py
Sample output:
{
"headers": {
"date": "Thu, 13 Jul 2023 08:04:35 GMT",
"content-type": "application/json;charset=utf-8",
"content-length": "151",
"connection": "keep-alive",
"keep-alive": "timeout=25",
"access-control-allow-origin": "*",
"access-control-expose-headers": "*",
"x-acs-request-id": "0E144D0A-B869-5451-AD12-16B56576****",
"x-acs-trace-id": "202b7b433bc249690b8efc7b2070****",
"etag": "1m1nrhLTvQdwdhKVR9B2****"
},
"statusCode": 200,
"body": {
"RequestId": "0E144D0A-B869-5451-AD12-16B56576****",
"ServiceStatus": {
"AccountId": "151266687691****",
"RegionsInUse": ["cn-shanghai"],
"Status": "Enabled"
}
}
}