通过RESTAPI从Python获取Azure队列的长度

通过RESTAPI从Python获取Azure队列的长度,python,rest,azure,Python,Rest,Azure,有人举过这样的例子吗 本页介绍我尝试使用的REST API函数: 本页介绍授权过程。不幸的是,它似乎没有指定如何形成适合上述API函数的授权字符串: 这是我试过的。我得到一个403“禁止”响应代码: import base64, datetime, hashlib, hmac, requests account_name = 'account123' account_key = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRS

有人举过这样的例子吗

本页介绍我尝试使用的REST API函数:

本页介绍授权过程。不幸的是,它似乎没有指定如何形成适合上述API函数的授权字符串:

这是我试过的。我得到一个403“禁止”响应代码:

import base64, datetime, hashlib, hmac, requests

account_name = 'account123'
account_key = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789abcdefghijklmn=='
queue_name = 'stuff-to-process'

resource = '/{}/{}?comp=metadata'.format(account_name, queue_name)
rfc1123date = datetime.datetime.utcnow().strftime('%a, %d %b %Y %H:%M:%S GMT')
x_headers = 'x-ms-date:' + rfc1123date
string_to_hash = 'GET\n\n\n\n' + x_headers + '\n' + resource
bytes_to_hash = bytes(string_to_hash).encode('utf-8')
decoded_key = base64.b64decode(account_key)
encoded_hash = base64.b64encode(hmac.new(decoded_key, bytes_to_hash, digestmod=hashlib.sha256).digest())

url = 'https://{}.queue.core.windows.net/{}'.format(account_name, queue_name)
headers = {
    'Authorization': 'SharedKeyLite ' + account_name + ':' + encoded_hash,
    'x-ms-date': rfc1123date
}

response = requests.get(url, params={'comp':'metadata'}, headers=headers, timeout=5)
print 'Response code was', response.status_code

我试图重现该问题,下面的错误响应通过
print response.text
显示了原因


密钥是不支持
身份验证方案SharedKeyLite。
因此请在代码的请求头
授权
中使用
SharedKey
而不是
SharedKeyLite
,然后它工作,您可以通过
response.header[“x-ms-approximate-messages-count”]
从response头
x-ms-approximate-messages-count
提取队列长度值,这是非常有用的信息。非常感谢。我会试试你说的。但是,我链接到的身份验证文档说,“您可以使用共享密钥Lite身份验证来验证针对2009-09-19及更高版本的Blob和Queue服务发出的请求”。他们是否错了,或者我是否需要明确请求最新版本?
<?xml version="1.0" encoding="utf-8"?>
<Error><Code>AuthenticationFailed</Code>
  <Message>Server failed to authenticate the request. Make sure the value of Authorization h
eader is formed correctly including the signature.
RequestId:927f38e8-0003-0023-76ad-c84979000000
Time:2017-05-09T10:14:33.8632801Z</Message>
  <AuthenticationErrorDetail>Authentication scheme SharedKeyLite is not supported.</AuthenticationErrorDetail>
</Error>