我要按照here中的说明进行CloudStack API的“ uploadVolume”调用。
我使用的是python,我的代码可用于所有其他API调用(uploadVolume除外)。
secretkey = 'EdKz8Po3zmFWacumfxirauUBPVcLEWKq0qRox1iqhzAsrmLnJaEJxL2IsyDfyPE2Oh5AODJG3ZGKZf6A08xQAw'
apikey = 'YIShhcJ1PaGF5awJlci46qyAqhEN8WYdtUodHAT2wp7-zpN21_fJpcy1POTiMXOlFH-f-rO3zAs3tYRGcFqxwg'
request['command']='uploadVolume'
request['format']='QCOW2'
request['name']='test'
request['url']='http://192.168.122.100/image.qcow2'
request['zoneid']='959416fc-71f0-461c-99f0-28449e098036'
request['response']='json'
request['apikey']= apikey
baseurl='http://192.168.122.250:8080/client/api?'
request_str='&'.join(['='.join([k,urllib.quote_plus(request[k])]) for k in request.keys()])
sig_str='&'.join(['='.join([k.lower(),urllib.quote_plus(request[k].lower().replace('+','%20'))])for k in sorted(request.iterkeys())])
sig=urllib.quote_plus(base64.encodestring(hmac.new(secretkey,sig_str,hashlib.sha1).digest()).strip())
url=baseurl+request_str+'&signature='+sig
生成的URL如下:
http://192.168.122.250:8080/client/api?apikey=YIShhcJ1PaGF5awJlci46qyAqhEN8WYdtUodHAT2wp7-zpN21_fJpcy1POTiMXOlFH-f-rO3zAs3tYRGcFqxwg&name=test&format=QCOW2&url=http%3A%2F%2F192.168.122.100%2Fimage.qcow2&zoneid=959416fc-71f0-461c-99f0-28449e098036&command=uploadVolume&response=json&signature=EKhoEWKIG3QcmFM9k6sVRruZvFs%3D
响应表明存在签名问题:
{"uploadvolumeresponse":{"uuidList":[],"errorcode":401,"errortext":"unable to verify user credentials and/or request signature"}}
经过一些测试,我发现由于url参数而导致了签名错误。如果我用此参数中的任何字符串替换url,则签名正确完成,但是(显然)命令失败(因为CloudStack将找不到该图像)。
我认为问题在于构成网址的特殊字符,但我不确定。
有人知道如何解决吗?