Hi,
I found an example online of using SAS to send to an Event Hub via JavaScript on a device.
I got this script to successfully post events, and then it stopped working.
Chrome Developer Tools Network says 401 (40104 Invalid authorization token audience.)
The Console message is:
XMLHttpRequest cannot load https://devhub01-ns.servicebus.windows.net/devhub02/publishers/pd1/messages. No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://localhost:16532' is therefore not allowed access. The response had HTTP status code 401.
What is strange is that it worked, and then stopped. Using this tool, I generated a new SAS token (a few times) , and the issue persists, despite the fact the a new SAS token seemed to be what got it working in the first place.
Here is the JavaScript (with the serviceNamespace and HubName changed to disable the SAS):
<input type="text" value="9" id="temp" /><input type="button" value="Send Temperature" onclick="sendTemperature()" /><label id="status"></label><script type="text/javascript"> function sendTemperature() { var sas = 'SharedAccessSignature sr=https%3a%2f%2fdevhub01-ns.servicebus.windows.net.servicebus.windows.net%2fdevhub02%2fpublishers%2fpd1%2fmessages&sig=gycTCE6nuqkbdG4GbDhfDkJ%2bLlZD%2b2s5X%2b12BSYCLcw%3d&se=1420704992&skn=Admin'; var serviceNamespace = "DevHub01-ns"; var hubName = "devhub02"; var deviceName = "pd1"; var xmlHttpRequest = new XMLHttpRequest(); xmlHttpRequest.open("POST", "https://" + serviceNamespace + ".servicebus.windows.net/" + hubName + "/publishers/" + deviceName + "/messages", true); xmlHttpRequest.setRequestHeader('Content-Type',"application/json"); xmlHttpRequest.setRequestHeader("Authorization", sas); xmlHttpRequest.onreadystatechange = function () { if (this.readyState == 4) { if (this.status == 201) { document.getElementById('status').innerText = 'Sent: ' + document.getElementById('temp').value; } else { document.getElementById('status').innerText = this.status; } } }; xmlHttpRequest.send('{"temperature":"20","area":"Frankfurt"}'); }</script>
Please let me know if you see a reason why this currently doesn't work.
Thanks,
Aron