IMS Python SDK call examples

Updated at:

Call the IMS GetUser API operation through the Python SDK to query the details of a RAM user.

Step 1: Review the OpenAPI documentation

In the API Overview, find the Query Detailed Information of a RAM User operation. Review the API documentation for required parameters and permissions.

Step 2: Create and authorize a RAM user

Identity

You can call this API by using an Alibaba Cloud account (main account), a RAM user, or a RAM role. For more information, see Identity.

This example uses a RAM user to call the API.

Log on to the Resource Access Management (RAM) console and create a RAM user. For more information, see Create a RAM user.

Credentials

Go to the details page of the target RAM user. On the Authentication tab, in the User AccessKeys section, click Create AccessKey. For more information, see Create an AccessKey.

Important

The AccessKey Secret of a RAM user is displayed only once when you create it. Save the AccessKey Secret in a secure location.

Authorization

  1. Go to the RAM Users page. In the Actions column for the target RAM user, click Grant Permissions.

  2. Based on the principle of least privilege, select an appropriate permission policy.

    • AliyunRAMFullAccess: Grants full management permissions for Resource Access Management (RAM).

    • AliyunRAMReadOnlyAccess: Grants read-only permissions for Resource Access Management (RAM).

    Note

    If the system policies do not meet your requirements, you can create a custom policy. For more information, see Create a custom permission policy and Authorization information.

    This example uses the AliyunRAMReadOnlyAccess system policy.

  3. Click OK.

Step 3: Call the API

This example uses the Python SDK. The process is similar for other language SDKs. For more information, see IMS SDK. You can also use other methods based on your business needs. For more information, see Calling Methods.

Prepare the Python environment

Download and install Python 3.

After installation, run python --version in your terminal to verify that Python is installed.

Configure environment variables

To avoid hard-coding AccessKey credentials in your code, this example stores the AccessKey pair in environment variables. For more information, see Configure environment variables on Linux, macOS, and Windows.

Install dependencies

pip install alibabacloud_credentials
pip install alibabacloud_ims20190815==3.0.1
pip install alibabacloud_tea_console

Download the sample code

  1. In the OpenAPI Portal, go to the GetUser debugging URL.

  2. Enter the request parameters and click Send Request to verify the call.

  3. On the SDK Sample tab, select the Python tab, and click Download Project.

  4. Extract the sample code package and navigate to the alibabacloud_sample directory.

Run the sample code

Run the following command:

python sample.py

The output is similar to the following:

{
	"headers": {
		"date": "Thu, 17 Aug 2023 09:07:54 GMT",
		"content-type": "application/json;charset=utf-8",
		"content-length": "344",
		"connection": "keep-alive",
		"keep-alive": "timeout=25",
		"access-control-allow-origin": "*",
		"access-control-expose-headers": "*",
		"x-acs-request-id": "CCDC0D83-7A3E-59F1-9A27-F04F267591DB",
		"x-acs-trace-id": "d98639642df3407007b84a6dc3f8bb0f",
		"etag": "316nsRQdGEB/4H8NITW6C/w4"
	},
	"statusCode": 200,
	"body": {
		"RequestId": "CCDC0D83-7A3E-59F1-9A27-F04F267591DB",
		"User": {
			"Comments": "This is a cloud computing engineer.",
			"CreateDate": "2021-05-06T07:37:48Z",
			"DisplayName": "vpc-dev",
			"Email": "",
			"LastLoginDate": "2022-05-06T07:37:48Z",
			"MobilePhone": "",
			"ProvisionType": "Manual",
			"Tags": {
				"Tag": []
			},
			"UpdateDate": "2021-05-06T07:37:48Z",
			"UserId": "22718132028666****",
			"UserPrincipalName": "vpc-dev@151266687691****.onaliyun.com"
		}
	}
}