Reputation: 33
am trying to retrieve a list of all devices registered in azure notification but i keep getting the same error
[
"401
MissingAudience: The provided token does not specify the 'Audience'..TrackingId:38d0f9f4-9836-498",
"2-a1d9-60f5ceed7210_G7,TimeStamp:1/28/2019 3:59:33 PM"
]
domain = "https://mnamespace1.servicebus.windows.net"
url = domain + "/notificationhub/registrations/?api-version=2017-11"
r = requests.get(url, headers = {
# "Content-Type": "application/atom+xml;type=entry;charset=utf-8",
"authorization": 'WRAP access_token ="QeXuRZO2ATZM8jolXFYPDvQb4zWSzifmWvpS8eb5FLM="',
"x-ms-version": "2017-11"})
print (Response(r))
Upvotes: 0
Views: 289
Reputation: 24148
According to your error information and code, the issue was caused by generating the sas token incorrectly.
Based on the description of REST API Read All Registrations
, the correct sas token for this api should be like below, please refer to the section Create SAS Security Token
to know how to generate.
SharedAccessSignature sr=https%3A%2F%2F<your servicebus name>.servicebus.windows.net%2F<your notification hub name>&sig=<signature with urlencode>&se=<expires>&skn=<police name, such as DefaultFullSharedAccessSignature>
The SAS token above is the same as for EventHub, so you can refer to the sample code in Python for EventHub to generate it, please refer to Generate SAS token
for Python to write for Notification Hub.
Here is my sample code.
import time
from urllib import parse
import hmac
import hashlib
import base64
def generate_sas_token(uri, sas_name, sas_value):
target_uri = parse.quote_plus(uri)
sas = sas_value.encode('utf-8')
expiry = str(int(time.time() + 10000))
string_to_sign = (target_uri + '\n' + expiry).encode('utf-8')
signed_hmac_sha256 = hmac.HMAC(sas, string_to_sign, hashlib.sha256)
signature = parse.quote(base64.b64encode(signed_hmac_sha256.digest()))
return 'SharedAccessSignature sr={}&sig={}&se={}&skn={}' \
.format(target_uri, signature, expiry, sas_name)
api_version = '2017-11'
uri = 'https://<your servicebus name>.servicebus.windows.net/<your notificationhub name>/registrations/?api-version={}'.format(api_version)
sas_name = '<SharedAccessKeyName>' # for example, DefaultFullSharedAccessSignature
sas_value = '<SharedAccessKey>'
sas_token = generate_sas_token(uri, sas_name, sas_token)
Then, you can get the response of the REST API as the code below.
import requests
headers = {
'Authorization':sas_token,
'x-ms-version':api_version
}
r = requests.get(uri, headers=headers)
print(r.text)
Upvotes: 1