I'm attempting to run a sample event generator to familiarize myself with Azure Event Hubs and Azure Stream Analytics.
import asyncio
from azure.eventhub.aio import EventHubProducerClient
from azure.eventhub import EventData
import json
import datetime
import uuid
import random
import time
async def run():
# Create a producer client to send messages to the event hub.
# Specify a connection string to your event hubs namespace and
# the event hub name.
producer = EventHubProducerClient.from_connection_string(conn_str="Endpoint=sb://myNamespace.servicebus.windows.net/;SharedAccessKeyName=EH-ASA-Access;SharedAccessKey=numbersAndLetterAndStuff=", eventhub_name="test_event (myNamespace/test_event)")
city_list = ["San Franciso", "San Jose", "Los Angesles", "Seattle","Austin", "Dallas", "Denver", "New York", "Atlanta", "Miami", "Phoenix", "Tempe"]
async with producer:
for i in range(0, 600):
# Create a batch.
event_data_batch = await producer.create_batch()
tripdetail1 = {'tripId': str(uuid.uuid4()), 'createdAt': str(datetime.datetime.utcnow()), 'startLocation': random.choice(city_list), 'endLocation': random.choice(city_list),
'distance': random.randint(10, 1000), 'fare': random.randint(100, 1000) }
tripdetail2 = {'tripId': str(uuid.uuid4()), 'createdAt': str(datetime.datetime.utcnow()), 'startLocation': random.choice(city_list), 'endLocation': random.choice(city_list),
'distance': random.randint(10, 1000), 'fare': random.randint(100, 1000) }
tripdetail3 = {'tripId': str(uuid.uuid4()), 'createdAt': str(datetime.datetime.utcnow()), 'startLocation': random.choice(city_list), 'endLocation': random.choice(city_list),
'distance': random.randint(10, 1000), 'fare': random.randint(100, 1000) }
print (tripdetail1);
print (tripdetail2);
print (tripdetail3);
# Add events to the batch.
event_data_batch.add(EventData(json.dumps(tripdetail1)))
event_data_batch.add(EventData(json.dumps(tripdetail2)))
event_data_batch.add(EventData(json.dumps(tripdetail3)))
event_data_batch.add(EventData(json.dumps(tripdetail1)))
# Send the batch of events to the event hub.
await producer.send_batch(event_data_batch)
time.sleep(1)
loop = asyncio.get_event_loop()
loop.run_until_complete(run())
But when I run the above file from my local machine I get:
\> python .\EventGenerator.py
Traceback (most recent call last):
File "C:\Users\blalonde\AppData\Local\Programs\Python\Python310\lib\site-packages\azure\eventhub\aio\_client_base_async.py", line 516, in _do_retryable_operation
return await operation()
File "C:\Users\blalonde\AppData\Local\Programs\Python\Python310\lib\site-packages\azure\eventhub\aio\_client_base_async.py", line 469, in _open
while not await self._handler.client_ready_async():
File "C:\Users\blalonde\AppData\Local\Programs\Python\Python310\lib\site-packages\azure\eventhub\_pyamqp\aio\_client_async.py", line 314, in client_ready_async
if not await self.auth_complete_async():
File "C:\Users\blalonde\AppData\Local\Programs\Python\Python310\lib\site-packages\azure\eventhub\_pyamqp\aio\_client_async.py", line 301, in auth_complete_async
if self._cbs_authenticator and not await self._cbs_authenticator.handle_token():
File "C:\Users\blalonde\AppData\Local\Programs\Python\Python310\lib\site-packages\azure\eventhub\_pyamqp\aio\_cbs_async.py", line 250, in handle_token
raise TokenAuthFailure(
azure.eventhub._pyamqp.error.TokenAuthFailure: CBS Token authentication failed.
Status code: None
From what I can tell, I have the SAS Key entered properly, I also did a refresh to make sure it was current. I've checked that my event-name is correct as well. I also tried to use the RootManageSharedAccessKey, but got the same error.