SampleCode
Last Updated:2020-07-20
The interface for querying an instance list is taken as an example for description. Please see the following example:
Python example
# -*- coding: utf-8 -*-
import json
import httplib
from datetime import datetime
import sign_sample # Authentication Computing Module refers to Baidu AI Cloud Authentication Mechanism
if __name__ == "__main__":
signer_obj = sign_sample.BceCredentials('730fxxxxxxxxxxxxxxx', '75xxxxxxxxxxxxxxxxxxxxxx') # ak, sk of Baidu AI Cloud Account
request = {
'method': 'GET',
'uri': '/v1/instance',
'params': {},
'headers': {
'host': 'drds.bj.baidubce.com', # DRDS openAPI的endpoint
'x-bce-date': datetime.utcnow(),
'x-bce-if-match': 'v1'
}
}
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())
Other references
Authentication and certification Sample Code: View Details.