如何从cosmosdb集合中检索特定文档,使用RESTAPI的有效负载和url是什么?

2024-04-19 15:40:53 发布

您现在位置:Python中文网/ 问答频道 /正文

我能够使用Python请求从cosmos db Mongo api检索数据库,但不知道如何获取文档。应该对url和负载进行哪些更改

import requests
import hmac
import hashlib
import base64
from datetime import datetime
import urllib.parse
key = 'FFwCOdmAy55XI2FqKDQX0qoQUeAweYMegGofQnCcBseVVKMhIE2UbzyJMTMbVOYTncaRa5HffqQTlscm20GHdA=='
now = datetime.utcnow().strftime('%a, %d %b %Y %H:%M:00 GMT')
print(now)
payload = ('get\ndbs\n\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/'
res = requests.get(url, headers = headers)
print(res.content)

Tags: keyimporturldatetimeurllibrequestshmacnow