代码示例
更新时间:2024-06-27
Python示例
以查询实例列表接口为例,进行说明。请查看以下示例:
# -*- coding: utf-8 -*-
import json
import httplib
from datetime import datetime
import sign_sample # 鉴权计算模块,参考百度智能云鉴权认证机制
if __name__ == "__main__":
signer_obj = sign_sample.BceCredentials('730fxxxxxxxxxxxxxxx', '75xxxxxxxxxxxxxxxxxxxxxx') #百度智能云账号的ak、sk
request = {
'method': 'GET',
'uri': '/v1/vdb/instance/list',
'params': {},
'headers': {
'host': 'vdb.bj.baidubce.com', # VDB openAPI的endpoint
'x-bce-date': datetime.utcnow(),
'content-type': 'application/json'
}
}
requestBody = {}
auth = sign_sample.sign(signer_obj, request["method"], request["uri"],request["headers"], request["params"],0,1800, request["headers"])
print "Authorization:" + auth
req = httplib.HTTPConnection(request["headers"]["host"])
request['headers']["Authorization"] = auth
req.request(request['method'], request['uri'], json.dumps(requestBody), request['headers'])
response = req.getresponse()
print "httpStatus: " + str(response.status)
print "header: " + str(response.getheaders())
print json.loads(response.read())
鉴权与认证Sample Code:查看详情