我想连接到我的 azure 存储资源管理器并使用 python 读取表数据。它在 Azure 存储模拟器上运行。我的代码是这样的;
storage_account_name = 'devstoreaccount1'
storage_account_key = 'Eby8vdM02xNOcqFlqUwJPLlmEtlCDXJ1OUzFT50uSRZ6IFsuFq2UVErCz4I6tq/K1SZFPTOtr/KBHBeksoGMGw=='
#api_version = '2016-05-31'
api_version = '2017-04-17'
request_time = datetime.datetime.now().strftime('%a, %d %b %Y %H:%M:%S GMT')
print(request_time)
string_params = {
'verb': 'GET',
'Content-MD5': '',
'Content-Type': '',
'Date': request_time,
'CanonicalizedResource': '/' + storage_account_name +'/'+storage_account_name + '\ncomp:list' #note, it should be '\ncomp:list', no '/'
}
string_to_sign = (string_params['verb'] + "\n" +
string_params['Content-MD5'] + "\n" +
string_params['Content-Type'] + "\n" +
string_params['Date'] + "\n" +
string_params['CanonicalizedResource'])
signed_string = base64.b64encode(hmac.new(base64.b64decode(storage_account_key), msg=string_to_sign.encode('utf-8'), digestmod=hashlib.sha256).digest()).decode()
headers = {
'Date' : request_time,
'x-ms-version' : api_version,
'Authorization' : ('SharedKey ' + storage_account_name + ':' + signed_string)
}
url = ('http://127.0.0.1:10002/' + storage_account_name + '?comp=list')
r = requests.get(url, headers = headers)
print(r.status_code)
print(r.content)
我提到了这个。[https://learn.microsoft.com/en-us/rest/api/storageservices/authorize-with-shared-key#constructing-the-canonicalized-headers-string:~:text=Table %20service%20(Shared%20Key%20authorization)]
但是,它给出了这个错误。
403
b'<?xml version="1.0" encoding="utf-8"?><m:error xmlns:m="http://schemas.microsoft.com/ado/2007/08/dataservices/metadata"><m:code>AuthenticationFailed</m:code><m:message xml:lang="en-US">Server failed to authenticate the request. Make sure the value of Authorization header is formed correctly including the signature.\nRequestId:fdcea5c3-e071-4745-87dd-620848f550de\nTime:2023-03-03T05:07:01.4244873Z</m:message></m:error>'
我尝试对签名字符串进行更改。但没有成功。
有人知道我的代码有什么问题吗?