How can I consume events from Event Grid Namespace Topic Subscription using HTTP

Sai k Marthi 20 Reputation points

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 .User's image

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><topic name>/eventsubscriptions/<topic subscription>:receive?api-version=2023-06-01-preview"
access_key = "<topic-key-from-namespace-topic"

namespace = ""

# 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 =, headers=headers)

# Check if the request was successful
if response.status_code == 200:
    print("Successfully received events.")
    print(response.json())  # Print the received events
    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

User's image Please guide me on how to do it or point me where I can find it.

Thank you,

M. Sai Kalyan.

Azure Event Grid
Azure Event Grid
An Azure event routing service designed for high availability, consistent performance, and dynamic scale.
332 questions
{count} votes

Accepted answer
  1. Ryan Hill 26,771 Reputation points Microsoft Employee

    Hi @Sai k Marthi,

    It's great to hear that you've resolved your issue, and posting your solution is a generous way to assist others who may encounter the same problem. As the Microsoft Q&A community guidelines state that the question author cannot accept their own answer but only those provided by others, I will repost your solution for you to accept if you wish.

    The issue was you are using a Python script/program to consume messages from an Event Grid through an HTTP endpoint. You were receiving a 401 message which alluded to an improper header value. You elected to change the Authorization header to use SharedAccessKey instead.

    0 comments No comments

0 additional answers

Sort by: Most helpful