3

在这被标记为重复之前:我已经阅读了关于 SO 的每个问题/答案以及上述错误消息,但没有一个能解决我的问题。我一定错过了一些简单的东西,因为应该简单的东西不起作用。

我创建了一个事件中心命名空间,在命名空间中有一个“发送”共享访问策略和一个事件中心。

使用Python Event Hub SDK中的代码(在另一个答案中建议),我有以下脚本来创建我的Authorization标题:

import time
from base64 import b64encode, b64decode
from hashlib import sha256
from hmac import HMAC
from urllib.parse import quote_plus, urlencode

def generate_sas_token(uri, policy, policy_key, expiry_days=14):
    expiry = time.time() + expiry_days * 60 * 60 * 24
    encoded_uri = quote_plus(uri)
    ttl = int(expiry)
    sign_key = '{}\n{}'.format(encoded_uri, ttl)
    signature = b64encode(HMAC(b64decode(policy_key), sign_key.encode('utf-8'), sha256).digest())
    result = {
        'sr': uri,
        'sig': signature,
        'se': str(ttl),
        'skn': policy
    }
    return 'SharedAccessSignature ' + urlencode(result)

if __name__ == '__main__':
    NAMESPACE = input('Namespace: ').strip().lower()
    URI = '{}.servicebus.windows.net'.format(NAMESPACE)
    POLICY = input('Policy: ').strip()
    POLICY_KEY = input('Policy key: ').strip()
    EXPIRY_DAYS = int(input('Expiry (days): ').strip())
    print(generate_sas_token(URI, POLICY, POLICY_KEY, EXPIRY_DAYS))

现在,如果我使用以下(虚拟)值运行此脚本:

NAMESPACE=<my Event Hub Namespace> # let's call it "ehns"
POLICY=<"Send" Shared Access Policy Name> # let's call it "event-publisher"
POLICY_KEY=<Primary Key for the above policy, ends with = sign>
EXPIRY_DAYS=14

然后我得到以下(虚拟)Authorization标题:

SharedAccessSignature sr=ehns.servicebus.windows.net&sig=xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx%3D&se=1574773874&skn=event-publisher

现在,当我在此页面之后向 API 端点发布一条虚拟消息时:

curl -i -X POST --data-ascii "test message" -H "Content-Type: text/plain" -H "Authorization: SharedAccessSignature sr=ehns.servicebus.windows.net&sig=xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx%3D&se=1574773874&skn=event-publisher" https://ehns.servicebus.windows.net/ehresource/messages

我得到以下回复:

HTTP/1.1 401 SubCode=40103: Invalid authorization token signature
Content-Length: 0
Server: Microsoft-HTTPAPI/2.0
Strict-Transport-Security: max-age=31536000
Date: Tue, 12 Nov 2019 13:11:34 GMT

笔记:

4

1 回答 1

4

请使用以下代码:

import time
import hmac
import hashlib
import base64
from urllib.parse import quote_plus, urlencode

def _sign_string(uri, key, key_name):

    '''
    100000 = milsecond expiry
    '''
    expiry = int(time.time() + 10000)

    string_to_sign = quote_plus(uri) + '\n' + str(expiry)

    key = key.encode('utf-8')
    string_to_sign = string_to_sign.encode('utf-8')
    signed_hmac_sha256 = hmac.HMAC(key, string_to_sign, hashlib.sha256)
    signature = signed_hmac_sha256.digest()
    signature = base64.b64encode(signature)

    return 'SharedAccessSignature sr=' + quote_plus(uri)  + '&sig=' + quote_plus(signature) + '&se=' + str(expiry) + '&skn=' + key_name


if __name__ == '__main__':    
    URI = "your_eventhub_namespace.servicebus.windows.net/your_eventhub_name"
    POLICY = "your_policy_name"
    POLICY_KEY = "the policy key"

    print(_sign_string(URI,POLICY_KEY,POLICY))

测试结果:

在此处输入图像描述

于 2019-11-13T02:38:14.130 回答