繁体   English   中英

如何为Azure Cosmos DB生成标头以进行其他api请求

[英]How to generate headers for azure cosmos db to make rest api requests

我想从我的azure cosmos db获取文档,但是我遇到了未经授权的错误,我知道它与资源ID和资源类型字符串格式有关。 请帮助构造正确的字符串。

import requests
import hmac
import hashlib
import base64
from datetime import datetime
import urllib.parse
key = '<key>=='
now = datetime.utcnow().strftime('%a, %d %b %Y %H:%M:00 GMT')
print(now)
payload = ('get\ndocs\ndbs/iot/colls/messages\n' + now + '\n\n').lower()


payload = bytes(((payload)),encoding='utf8')
key = base64.b64decode(key.encode('utf-8'))

signature = base64.b64encode(hmac.new(key, msg = payload, digestmod = 
hashlib.sha256).digest()).decode()
print(signature)

authStr = urllib.parse.quote('type=master&ver=1.0&sig={}'.format(signature))
print("key = " ,authStr)

headers = {
'Authorization': authStr,
"x-ms-date": now,
"x-ms-version": "2017-02-22"
}
url = 'https://rpi.documents.azure.com/dbs/iot/colls/messages'
res = requests.get(url, headers = headers)
print(res.content)

我注意到您的有效负载中有一个额外的换行符(在messages之后)

payload = ('get\ndocs\ndbs\iot\colls\messages\n\n' + now + '\n\n').lower()

此外, resourceLink也似乎不正确。 您在资源链接中使用\\而不是/

您可以尝试将其更改为:

payload = ('get\ndocs\ndbs/iot/colls/messages\n' + now + '\n\n').lower()

另外,请将您的urlurl = 'https://rpi.documents.azure.com/dbs'更改为url = 'https://rpi.documents.azure.com/dbs/iot/colls/messages'

看看是否有区别。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM