This topic describes how to install Security Token Service (STS) SDK for Python and provides an example about how to use STS SDK for Python.

Background information

  • To use STS SDK for Python, you must install the core library of Alibaba Cloud SDK for Python (aliyun-python-sdk-core) and the STS SDK (aliyun-python-sdk-sts).
  • Alibaba Cloud provides OpenAPI Explorer to simplify API usage. You can use OpenAPI Explorer to debug API operations and dynamically generate SDK sample code.
  • For more information about STS API operations, see What is STS?.
  • For more information about STS endpoints, see Endpoints.

Install the SDK for Python

You can download the installation packages of the SDKs for Python from the following links:

Example

The following sample code provides an example on how to call the AssumeRole operation by using the SDK for Python. For other operations, you can use OpenAPI Explorer to debug the operations and obtain sample code.

#!/usr/bin/env python
#coding=utf-8

from aliyunsdkcore.client import AcsClient
from aliyunsdkcore.acs_exception.exceptions import ClientException
from aliyunsdkcore.acs_exception.exceptions import ServerException
from aliyunsdksts.request.v20150401.AssumeRoleRequest import AssumeRoleRequest

# Construct an Alibaba Cloud client to initiate requests. 
# Specify the AccessKey ID and AccessKey secret of the requester. The requester can be a Resource Access Management (RAM) user or a RAM role. 
client = AcsClient('<accessKeyId>', '<accessSecret>', 'cn-hangzhou')

# Construct a request. 
request = AssumeRoleRequest()
request.set_accept_format('json')

# Specify request parameters. For more information about the parameters, see API Reference. 
request.set_RoleArn("<RoleArn>")
request.set_RoleSessionName("<RoleSessionName>")

# Initiate the request and obtain a response. 
response = client.do_action_with_exception(request)
# python2:  print(response)
print(str(response, encoding='utf-8'))