本文介绍Python SDK初始化Client的流程。
系统变量读取
阿里云SDK支持通过定义ALIBABA_CLOUD_ACCESS_KEY_ID和ALIBABA_CLOUD_ACCESS_KEY_SECRET环境变量来创建默认的访问凭证。调用接口时,程序直接访问凭证,读取您的访问密钥(即AccessKey)并自动完成鉴权。详细配置方式,请参见在Linux、macOS和Windows系统配置环境变量。
初始化
创建AcsClient实例。
# 读取环境变量中设置的ACCESS_KEY ACCESS_KEY_SECRET信息 credentials = AccessKeyCredential(os.environ['ALIBABA_CLOUD_ACCESS_KEY_ID'], os.environ['ALIBABA_CLOUD_ACCESS_KEY_SECRET']) # region_id 为调用的服务地域ID,支持的地域参考 https://help.aliyun.com/document_detail/43248.html client = AcsClient(region_id='cn-shanghai', credential=credentials)
创建request,并设置参数。
# 以查询管道接口为例 request = QueryPipelineListRequest() request.set_PipelineIds("9bad31a2b3c22c4df9c6****") request.set_accept_format('json')
发起API请求并显示返回值。
response = client.do_action_with_exception(request) # 输出打印 print(str(response, encoding='utf-8'))
完整代码
import os
from aliyunsdkcore.client import AcsClient
from aliyunsdkcore.acs_exception.exceptions import ClientException
from aliyunsdkcore.acs_exception.exceptions import ServerException
from aliyunsdkcore.auth.credentials import AccessKeyCredential
from aliyunsdkmts.request.v20140618.QueryPipelineListRequest import QueryPipelineListRequest
# 读取环境变量中设置的ACCESS_KEY ACCESS_KEY_SECRET信息
credentials = AccessKeyCredential(os.environ['ALIBABA_CLOUD_ACCESS_KEY_ID'], os.environ['ALIBABA_CLOUD_ACCESS_KEY_SECRET'])
# region_id 为调用的服务地域ID,支持的地域参考 https://help.aliyun.com/document_detail/43248.html
client = AcsClient(region_id = 'cn-shanghai', credential = credentials)
request = QueryPipelineListRequest()
request.set_PipelineIds("9*********61c22c4df9c6")
request.set_accept_format('json')
response = client.do_action_with_exception(request)
# 输出打印
print(str(response, encoding='utf-8'))
文档内容是否对您有帮助?