Python SDK call example
This topic shows how to use the Python SDK to call the Service Catalog ListPortfolios OpenAPI operation to query product portfolios.
Step 1: Review the OpenAPI documentation
Read the API overview and select the ListPortfolios OpenAPI operation to query product portfolios. Review the API documentation to understand the required parameters and permissions for this operation.
Step 2: Create and authorize a RAM user
Identity
You can use an Alibaba Cloud account, a RAM user, or a RAM role to call this operation. To learn about the differences between these identities, see Identities.
This example uses a RAM user to call the operation.
Log on to the Resource Access Management (RAM) console and create a RAM user for OpenAPI calls only. This process generates an AccessKey pair. Be sure to save it. For detailed instructions, see Create a RAM user.
Authorization
Console
-
Log on to the RAM console.
-
On the Users page, find the user you created, and in the Operation column, click Add Permissions.
-
In the Add Permissions panel, grant permissions to the RAM user.
-
For Authorized Scope, select Alibaba Cloud Account.
-
For Permission Policy, enter the keyword
servicecatalogin the search box. Select a permission policy based on the principle of least privilege.-
AliyunServiceCatalogAdminFullAccess: Grants administrative permissions for Service Catalog.
-
AliyunServiceCatalogAdminReadOnlyAccess: Grants read-only administrative permissions for Service Catalog.
NoteIf a system policy cannot meet your requirements, you can create a custom policy. For more information, see Create a custom permission policy and Grant permissions to a RAM user.
This example uses the AliyunServiceCatalogAdminReadOnlyAccess system policy.
-
-
-
Click OK.
API
Grant a custom policy
-
Call the CreatePolicy operation to create a custom policy. For more information, see Policy elements and Policy examples.
-
Call the AttachPolicyToUser operation to attach the permission policy to the specified RAM user. Set the
PolicyTypeparameter toCustom.
Grant a system policy
Call the AttachPolicyToUser operation to attach a permission policy to the specified RAM user. Set the PolicyType parameter to System and the PolicyName parameter to AliyunServiceCatalogAdminFullAccess or AliyunServiceCatalogAdminReadOnlyAccess
-
AliyunServiceCatalogAdminFullAccess: Grants administrative permissions for Service Catalog.
-
AliyunServiceCatalogAdminReadOnlyAccess: Grants read-only administrative permissions for Service Catalog.
This example uses the AliyunServiceCatalogAdminReadOnlyAccess system policy.
Credentials
Go to the details page of the RAM user. On the Authentication tab, view the AccessKey pair in the User AccessKeys section. You can also click Create AccessKey to create a new AccessKey pair. For detailed instructions, see Create an AccessKey pair.
The AccessKey secret of a RAM user is displayed only when the AccessKey pair is created. It cannot be retrieved later. Make sure to save it in a secure location.
Step 3: Call the API operation
This topic provides an example of using the Python SDK to call an OpenAPI operation. The process is similar for SDKs in other languages. For more information, see Service Catalog SDK. You can also choose other methods to call the operation based on your business requirements. For more information, see Invocation methods.
Prepare the Python environment
Download and install Python 3.
After installation, run python --version in your terminal to check the Python version.
Configure environment variables
To avoid security risks that may arise from hardcoding your AccessKey pair into your business code, this example uses environment variables to manage your AccessKey pair. For detailed instructions, see Configure credentials in environment variables on Linux, macOS, and Windows.
Install dependencies
pip install alibabacloud_credentials
pip install alibabacloud_servicecatalog20210901==1.1.4
pip install alibabacloud_tea_console
Download the sample code
-
In OpenAPI Explorer, go to the ListPortfolios debugging page.
-
Enter the request parameters and click Initiate Call to verify that the request is successful.
-
On the SDK Example tab, select the Python tab, and click Download the complete project. to download the Python SDK sample code package.
-
Extract the sample code package on your computer and navigate to the alibabacloud_sample directory.
Run the sample code
Run the following command:
python sample.py
The following output is returned:
{
"headers": {
"date": "Fri, 14 Jul 2023 06:29:19 GMT",
"content-type": "application/json;charset=utf-8",
"content-length": "577",
"connection": "keep-alive",
"keep-alive": "timeout=25",
"access-control-allow-origin": "*",
"access-control-expose-headers": "*",
"x-acs-request-id": "A453CDA5-B557-57A5-A05B-48F14646****",
"x-acs-trace-id": "131ed76192b0df2396bb6fb51342****",
"etag": "5+OC5aIgno6FJurD4O1s****"
},
"statusCode": 200,
"body": {
"PageNumber": 1,
"PageSize": 10,
"PortfolioDetails": [{
"CreateTime": "2023-07-14T06:11:31Z",
"Description": "",
"PortfolioArn": "acs:servicecatalog:cn-hangzhou:151266687691****:portfolio/port-bp1ac4uk39****",
"PortfolioId": "port-bp1ac4uk39****",
"PortfolioName": "test",
"ProviderName": "test"
}, {
"CreateTime": "2022-09-23T01:32:59Z",
"Description": "",
"PortfolioArn": "acs:servicecatalog:cn-hangzhou:151266687691****:portfolio/port-bp1rfbwd2f****",
"PortfolioId": "port-bp1rfbwd2f****",
"PortfolioName": "abc",
"ProviderName": "abc"
}],
"RequestId": "A453CDA5-B557-57A5-A05B-48F1464622A4",
"TotalCount": 2
}
}