azure-event-hubs-for-kafka
azure-event-hubs-for-kafka copied to clipboard
Request for python and c# sample about using Azure Event Hubs for Apache Kafka Ecosystems with Managed Identity OAuth
Hi team, can you provide us a python and c# sample just like this? Send and Receive Messages in Java using Azure Event Hubs for Apache Kafka Ecosystems with Managed Identity OAuth
I’m new to Azure and curios as to why the Java sample is the only one that provides both an app secret example and a managed identity example. Also, why would a user select one auth method over the other? Which method do more users prefer?
Yes it would be helpful to have a python example
Here's some code that I wrote that illustrates OAuth in Python (but not managed identity).
import logging
import msal
logger = logging.getLogger(__name__)
class AzureActiveDirectoryOauthBearer(object):
AUTHORITY_TEMPLATE = "https://login.microsoftonline.com/{}/"
AAD_TENANT_ID = "aad.tenant.id"
AAD_CLIENT_ID = "aad.client.id"
AAD_CLIENT_SECRET = "aad.client.secret"
def __init__(self, tenant_id, client_id, client_secret, scopes):
authority = self.AUTHORITY_TEMPLATE.format(tenant_id)
self.app = msal.ConfidentialClientApplication(
client_id,
authority=authority,
client_credential=client_secret,
)
self.scopes = scopes
def __call__(self, _config):
result = self.app.acquire_token_silent(self.scopes, account=None)
if not result:
logging.info("No suitable token exists in cache. Let's get a new one from AAD.")
result = self.app.acquire_token_for_client(self.scopes)
if "access_token" in result:
logger.debug("Access token %s... (expires %s)",
result["access_token"][:10], result["expires_in"])
return result["access_token"], time.time() + float(result["expires_in"])
else:
logging.debug(result)
msg = "Failed to get Auth from Active Directory:\n{}".format(
result["error_description"]
)
raise RuntimeError(msg)