I've been doing some testing with azure/azure-storage-python/azure-storage-common/azure/storage/common/_auth.py on both Python 2.7 installations and my own python 3.7 and I'm wondering what kind of string does the API server expect for the request.header's
Authorization field?
The code in question:
def _add_authorization_header(self, request, string_to_sign):
try:
signature = _sign_string(self.account_key, string_to_sign)
auth_string = 'SharedKey ' + self.account_name + ':' + signature
request.headers['Authorization'] = auth_string
except Exception as ex:
# Wrap any error that occurred as signing error
# Doing so will clarify/locate the source of problem
raise _wrap_exception(ex, AzureSigningError)
In Python2.7, the Authorization field value is a Unicode value : i.e. u":"; but in Python 3.7, I just get a ":
"
In Python 3.x, strings are already Unicode (though it doesn't show that when I do a print after the "auth_string = ..." line, i.e.
def _add_authorization_header(self, request, string_to_sign):
try:
signature = _sign_string(self.account_key, string_to_sign)
auth_string = 'SharedKey ' + self.account_name + ':' + signature
print(auth_string)
request.headers['Authorization'] = auth_string
except Exception as ex:
# Wrap any error that occurred as signing error
# Doing so will clarify/locate the source of problem
raise _wrap_exception(ex, AzureSigningError)
In 2.7, I get u'SharedKey :'; but in 3.7, I get 'SharedKey :'.
The former works. The later gives me:
Traceback (most recent call last):
File "./test2.py", line 189, in
print(bcx.exists('vhds'))
File "C:\Program Files\Git\python37\Lib\site-packages\azure\storage\blob\baseblobservice.py", line 1686, in exists
_dont_fail_not_exist(ex)
File "C:\Program Files\Git\python37\Lib\site-packages\azure\storage\common\_error.py", line 97, in _dont_fail_not_exist
raise error
File "C:\Program Files\Git\python37\Lib\site-packages\azure\storage\blob\baseblobservice.py", line 1682, in exists
self._perform_request(request, expected_errors=expected_errors)
File "C:\Program Files\Git\python37\Lib\site-packages\azure\storage\common\storageclient.py", line 430, in _perform_request
raise ex
File "C:\Program Files\Git\python37\Lib\site-packages\azure\storage\common\storageclient.py", line 358, in _perform_request
raise ex
File "C:\Program Files\Git\python37\Lib\site-packages\azure\storage\common\storageclient.py", line 344, in _perform_request
HTTPError(response.status, response.message, response.headers, response.body))
File "C:\Program Files\Git\python37\Lib\site-packages\azure\storage\common\_error.py", line 115, in _http_error_handler
raise ex
azure.common.AzureHttpError: Server failed to authenticate the request. Make sure the value of Authorization header is formed correctly including the signature.
AuthenticationFailed
Server failed to authenticate the request. Make sure the value of Authorization header is formed correctly including the signature.
RequestId:[redacted]
Time:2019-03-06T14:57:37.5078291Z
Am I missing something?
Thanks
Ed
解决方案Hi,
Here's a very basic script:
acct_name = ""
acct_key = "" # from storage account's Access Key list (Used key1)
blck = BlockBlobService(account_name=acct_name,
account_key=acct_key)
blck.exists("testing") # it exists in the storage acct.
Then I get the same error.
The same code run under Python 2.7works.