Hi Everyone,
I am trying to achieve a small task which is pulling an event from Event Grid Namespace Topic Subscription which is enabled for queue type pull model event consumption. The network I formed is as below.
I am sure that the events are reaching till namespace topic and I am sure that Event subscription is also working because when I use curl to consume the event as explained in this link .
But, I am trying to use below python code to consume the events.
import requests
from base64 import b64encode,b64decode
from hmac import HMAC
import urllib
import hashlib
import datetime
# Define your Event Grid namespace topic endpoint and access key
namespace_topic_endpoint = "https://<name-space-name>.<area>.eventgrid.azure.net/topics/<topic name>/eventsubscriptions/<topic subscription>:receive?api-version=2023-06-01-preview"
access_key = "<topic-key-from-namespace-topic"
namespace = "https://eztrnamespace.eastasia-1.eventgrid.azure.net"
# Create a function to generate the SAS token
def generate_sas_token(uri, key, expiry=3600):
ttl = datetime.datetime.utcnow() + datetime.timedelta(seconds=expiry)
encoded_resource = urllib.parse.quote_plus(uri)
encoded_expiration_utc = urllib.parse.quote_plus(ttl.isoformat())
unsigned_sas = f'r={encoded_resource}&e={encoded_expiration_utc}'
signature = b64encode(HMAC(b64decode(key), unsigned_sas.encode('utf-8'), hashlib.sha256).digest())
encoded_signature = urllib.parse.quote_plus(signature)
token = f'r={encoded_resource}&e={encoded_expiration_utc}&s={encoded_signature}'
return token
# Generate the SAS token
sas_token = generate_sas_token(namespace, access_key)
# Define the headers for the request
headers = {
"Content-Type": "application/json",
"Authorization":f"SharedAccessSignature {sas_token}"
}
params = { "Authorization":f"SharedAccessSignature {sas_token}"}
# Send the HTTP GET request to receive events
response = requests.post(namespace_topic_endpoint, headers=headers)
# Check if the request was successful
if response.status_code == 200:
print("Successfully received events.")
print(response.json()) # Print the received events
else:
print(f"Failed to receive events. Status code: {response.status_code}, Response: {response.text}")
There is no Luck in getting the data. I am facing error as
Please guide me on how to do it or point me where I can find it.
Thank you,
M. Sai Kalyan.