簡體   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