<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>topic Re: Adding a message to azure service bus in Data Engineering</title>
    <link>https://community.databricks.com/t5/data-engineering/adding-a-message-to-azure-service-bus/m-p/139454#M51205</link>
    <description>&lt;P class="qt3gz91 paragraph"&gt;It looks like the issue is with the Azure credential chain rather than Service Bus itself; in Databricks notebooks, DefaultAzureCredential won’t succeed unless there’s a valid identity available (env vars, CLI login, managed identity, or a Databricks “service credential” in Unity Catalog).&lt;/P&gt;
&lt;H3 class="_7uu25p0 qt3gz9c _7pq7t612 heading3 _7uu25p1"&gt;Why DefaultAzureCredential fails in Databricks&lt;/H3&gt;
&lt;UL class="qt3gz97 qt3gz92"&gt;
&lt;LI class="qt3gz9a"&gt;
&lt;P class="qt3gz91 paragraph"&gt;&lt;STRONG&gt;No Azure identity present&lt;/STRONG&gt; on the cluster: DefaultAzureCredential tries environment variables, then managed identity, then Azure CLI, etc.; without one configured, token acquisition fails immediately.&lt;/P&gt;
&lt;/LI&gt;
&lt;LI class="qt3gz9a"&gt;
&lt;P class="qt3gz91 paragraph"&gt;In Databricks, the recommended way to call Azure SDKs from notebooks is to use &lt;STRONG&gt;Unity Catalog Service Credentials&lt;/STRONG&gt; (which wrap a managed identity or service principal) and pass that to the Azure SDK client, rather than instantiating DefaultAzureCredential directly.&lt;/P&gt;
&lt;/LI&gt;
&lt;/UL&gt;
&lt;H3 class="_7uu25p0 qt3gz9c _7pq7t612 heading3 _7uu25p1"&gt;Two working approaches&lt;/H3&gt;
&lt;H4 class="_7uu25p0 qt3gz9c _7pq7t612 heading4 _7uu25p1"&gt;1) Use a Unity Catalog Service Credential (recommended)&lt;/H4&gt;
&lt;P class="qt3gz91 paragraph"&gt;This avoids storing secrets and works well from Databricks notebooks.&lt;/P&gt;
&lt;P class="qt3gz91 paragraph"&gt;High-level steps:&lt;/P&gt;
&lt;UL class="qt3gz97 qt3gz92"&gt;
&lt;LI class="qt3gz9a"&gt;
&lt;P class="qt3gz91 paragraph"&gt;Create a &lt;STRONG&gt;Service Credential&lt;/STRONG&gt; in Unity Catalog backed by an Azure managed identity via the Databricks Access Connector, and grant it rights to your Service Bus namespace/topic (Send permission on the topic or namespace).&lt;/P&gt;
&lt;/LI&gt;
&lt;LI class="qt3gz9a"&gt;
&lt;P class="qt3gz91 paragraph"&gt;In your notebook, retrieve the credential with &lt;CODE class="qt3gz9f"&gt;dbutils.credentials.getServiceCredentialsProvider('&amp;lt;service-credential-name&amp;gt;')&lt;/CODE&gt; and pass it to the Service Bus client.&lt;/P&gt;
&lt;/LI&gt;
&lt;/UL&gt;
&lt;P class="qt3gz91 paragraph"&gt;Example (async):&lt;/P&gt;
&lt;DIV class="go8b9g1 _7pq7t6cl" data-ui-element="code-block-container"&gt;
&lt;PRE&gt;&lt;CODE class="markdown-code-python qt3gz9e hljs language-python _1ymogdh2"&gt;%pip install azure-servicebus==&lt;SPAN class="hljs-number"&gt;7.12&lt;/SPAN&gt;&lt;SPAN class="hljs-number"&gt;.1&lt;/SPAN&gt; azure-identity==&lt;SPAN class="hljs-number"&gt;1.17&lt;/SPAN&gt;&lt;SPAN class="hljs-number"&gt;.0&lt;/SPAN&gt;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;DIV class="go8b9g3 _7pq7t62y _7pq7t6cm _7pq7t6ay _7pq7t6bo"&gt;
&lt;DIV class="go8b9g5 _7pq7t6cj"&gt;python&lt;/DIV&gt;
&lt;DIV class="_17yk06p0"&gt;&amp;nbsp;&lt;/DIV&gt;
&lt;/DIV&gt;
&lt;/DIV&gt;
&lt;DIV class="go8b9g1 _7pq7t6cl" data-ui-element="code-block-container"&gt;
&lt;PRE&gt;&lt;CODE class="markdown-code-python qt3gz9e hljs language-python _1ymogdh2"&gt;&lt;SPAN class="hljs-keyword"&gt;import&lt;/SPAN&gt; nest_asyncio, asyncio
nest_asyncio.apply()

&lt;SPAN class="hljs-keyword"&gt;from&lt;/SPAN&gt; azure.servicebus &lt;SPAN class="hljs-keyword"&gt;import&lt;/SPAN&gt; ServiceBusMessage
&lt;SPAN class="hljs-keyword"&gt;from&lt;/SPAN&gt; azure.servicebus.aio &lt;SPAN class="hljs-keyword"&gt;import&lt;/SPAN&gt; ServiceBusClient
&lt;SPAN class="hljs-comment"&gt;# IMPORTANT: use UC Service Credential provider, not DefaultAzureCredential&lt;/SPAN&gt;
credential = dbutils.credentials.getServiceCredentialsProvider(&lt;SPAN class="hljs-string"&gt;"my-service-credential"&lt;/SPAN&gt;)  &lt;SPAN class="hljs-comment"&gt;# name in UC&lt;/SPAN&gt;

FULLY_QUALIFIED_NAMESPACE = &lt;SPAN class="hljs-string"&gt;"xxx.servicebus.windows.net"&lt;/SPAN&gt;
TOPIC_NAME = &lt;SPAN class="hljs-string"&gt;"xxoutbound"&lt;/SPAN&gt;

&lt;SPAN class="hljs-keyword"&gt;async&lt;/SPAN&gt; &lt;SPAN class="hljs-keyword"&gt;def&lt;/SPAN&gt; &lt;SPAN class="hljs-title function_"&gt;send_single_message&lt;/SPAN&gt;(&lt;SPAN class="hljs-params"&gt;sender&lt;/SPAN&gt;):
    message = ServiceBusMessage(&lt;SPAN class="hljs-string"&gt;"Single Message"&lt;/SPAN&gt;)
    &lt;SPAN class="hljs-keyword"&gt;await&lt;/SPAN&gt; sender.send_messages(message)
    &lt;SPAN class="hljs-built_in"&gt;print&lt;/SPAN&gt;(&lt;SPAN class="hljs-string"&gt;"Sent a single message"&lt;/SPAN&gt;)

&lt;SPAN class="hljs-keyword"&gt;async&lt;/SPAN&gt; &lt;SPAN class="hljs-keyword"&gt;def&lt;/SPAN&gt; &lt;SPAN class="hljs-title function_"&gt;run&lt;/SPAN&gt;():
    &lt;SPAN class="hljs-keyword"&gt;async&lt;/SPAN&gt; &lt;SPAN class="hljs-keyword"&gt;with&lt;/SPAN&gt; ServiceBusClient(
        fully_qualified_namespace=FULLY_QUALIFIED_NAMESPACE,
        credential=credential,
        logging_enable=&lt;SPAN class="hljs-literal"&gt;True&lt;/SPAN&gt;,
    ) &lt;SPAN class="hljs-keyword"&gt;as&lt;/SPAN&gt; servicebus_client:
        sender = servicebus_client.get_topic_sender(topic_name=TOPIC_NAME)
        &lt;SPAN class="hljs-keyword"&gt;async&lt;/SPAN&gt; &lt;SPAN class="hljs-keyword"&gt;with&lt;/SPAN&gt; sender:
            &lt;SPAN class="hljs-keyword"&gt;await&lt;/SPAN&gt; send_single_message(sender)

asyncio.run(run())&lt;/CODE&gt;&lt;/PRE&gt;
&lt;/DIV&gt;
&lt;P class="qt3gz91 paragraph"&gt;Notes:&lt;/P&gt;
&lt;UL class="qt3gz97 qt3gz92"&gt;
&lt;LI class="qt3gz9a"&gt;
&lt;P class="qt3gz91 paragraph"&gt;The UC service credential object must have permission to send to the topic; assign RBAC (e.g., Azure Service Bus “Data Sender” role) to the managed identity attached to the Access Connector on the Service Bus resource.&lt;/P&gt;
&lt;/LI&gt;
&lt;LI class="qt3gz9a"&gt;
&lt;P class="qt3gz91 paragraph"&gt;If you prefer setting a default service credential for the cluster, you can set &lt;CODE class="qt3gz9f"&gt;DATABRICKS_DEFAULT_SERVICE_CREDENTIAL_NAME&lt;/CODE&gt; as an environment variable on the cluster and then use SDK defaults, but naming it explicitly in code is more portable.&lt;/P&gt;
&lt;/LI&gt;
&lt;/UL&gt;
&lt;H4 class="_7uu25p0 qt3gz9c _7pq7t612 heading4 _7uu25p1"&gt;2) Use DefaultAzureCredential with an actual identity source&lt;/H4&gt;
&lt;P class="qt3gz91 paragraph"&gt;If you still want DefaultAzureCredential, ensure one of its sources is available on the cluster:&lt;/P&gt;
&lt;P class="qt3gz91 paragraph"&gt;Options:&lt;/P&gt;
&lt;UL class="qt3gz97 qt3gz92"&gt;
&lt;LI class="qt3gz9a"&gt;
&lt;P class="qt3gz91 paragraph"&gt;Configure &lt;STRONG&gt;environment variables&lt;/STRONG&gt; for a service principal: &lt;CODE class="qt3gz9f"&gt;AZURE_CLIENT_ID&lt;/CODE&gt;, &lt;CODE class="qt3gz9f"&gt;AZURE_TENANT_ID&lt;/CODE&gt;, &lt;CODE class="qt3gz9f"&gt;AZURE_CLIENT_SECRET&lt;/CODE&gt; on the cluster, and grant that SP sender permissions on Service Bus.&lt;/P&gt;
&lt;/LI&gt;
&lt;LI class="qt3gz9a"&gt;
&lt;P class="qt3gz91 paragraph"&gt;Attach a &lt;STRONG&gt;managed identity&lt;/STRONG&gt; to the Databricks Access Connector and let DefaultAzureCredential pick it up if you also set it as the default service credential for the cluster. In Databricks classic compute, you can set &lt;CODE class="qt3gz9f"&gt;DATABRICKS_DEFAULT_SERVICE_CREDENTIAL_NAME&lt;/CODE&gt;, then simply use the Azure SDK without passing a credential explicitly.&lt;/P&gt;
&lt;/LI&gt;
&lt;/UL&gt;
&lt;P class="qt3gz91 paragraph"&gt;Example (DefaultAzureCredential after setting identity):&lt;/P&gt;
&lt;DIV class="go8b9g1 _7pq7t6cl" data-ui-element="code-block-container"&gt;
&lt;PRE&gt;&lt;CODE class="markdown-code-python qt3gz9e hljs language-python _1ymogdh2"&gt;&lt;SPAN class="hljs-keyword"&gt;from&lt;/SPAN&gt; azure.identity.aio &lt;SPAN class="hljs-keyword"&gt;import&lt;/SPAN&gt; DefaultAzureCredential
&lt;SPAN class="hljs-keyword"&gt;from&lt;/SPAN&gt; azure.servicebus.aio &lt;SPAN class="hljs-keyword"&gt;import&lt;/SPAN&gt; ServiceBusClient
&lt;SPAN class="hljs-keyword"&gt;from&lt;/SPAN&gt; azure.servicebus &lt;SPAN class="hljs-keyword"&gt;import&lt;/SPAN&gt; ServiceBusMessage
&lt;SPAN class="hljs-keyword"&gt;import&lt;/SPAN&gt; asyncio, nest_asyncio
nest_asyncio.apply()

FULLY_QUALIFIED_NAMESPACE = &lt;SPAN class="hljs-string"&gt;"xxx.servicebus.windows.net"&lt;/SPAN&gt;
TOPIC_NAME = &lt;SPAN class="hljs-string"&gt;"xxoutbound"&lt;/SPAN&gt;

credential = DefaultAzureCredential()

&lt;SPAN class="hljs-keyword"&gt;async&lt;/SPAN&gt; &lt;SPAN class="hljs-keyword"&gt;def&lt;/SPAN&gt; &lt;SPAN class="hljs-title function_"&gt;run&lt;/SPAN&gt;():
    &lt;SPAN class="hljs-keyword"&gt;async&lt;/SPAN&gt; &lt;SPAN class="hljs-keyword"&gt;with&lt;/SPAN&gt; ServiceBusClient(FULLY_QUALIFIED_NAMESPACE, credential=credential, logging_enable=&lt;SPAN class="hljs-literal"&gt;True&lt;/SPAN&gt;) &lt;SPAN class="hljs-keyword"&gt;as&lt;/SPAN&gt; sb:
        sender = sb.get_topic_sender(topic_name=TOPIC_NAME)
        &lt;SPAN class="hljs-keyword"&gt;async&lt;/SPAN&gt; &lt;SPAN class="hljs-keyword"&gt;with&lt;/SPAN&gt; sender:
            &lt;SPAN class="hljs-keyword"&gt;await&lt;/SPAN&gt; sender.send_messages(ServiceBusMessage(&lt;SPAN class="hljs-string"&gt;"hello"&lt;/SPAN&gt;))

    &lt;SPAN class="hljs-keyword"&gt;await&lt;/SPAN&gt; credential.close()

asyncio.run(run())&lt;/CODE&gt;&lt;/PRE&gt;
&lt;/DIV&gt;
&lt;P class="qt3gz91 paragraph"&gt;Common pitfall in your snippet:&lt;/P&gt;
&lt;UL class="qt3gz97 qt3gz92"&gt;
&lt;LI class="qt3gz9a"&gt;&lt;CODE class="qt3gz9f"&gt;get_token('xxx')&lt;/CODE&gt; is not a valid scope for Azure AD; when testing, use a real scope (for Service Bus you typically let the client fetch tokens internally, you don’t need to call &lt;CODE class="qt3gz9f"&gt;get_token&lt;/CODE&gt; yourself). If you do test, use a resource scope like &lt;CODE class="qt3gz9f"&gt;"&lt;A href="https://servicebus.azure.net/.default" target="_blank"&gt;https://servicebus.azure.net/.default&lt;/A&gt;"&lt;/CODE&gt; or the Azure SDK will handle it automatically when you send messages. But with UC service credentials, you usually don’t call &lt;CODE class="qt3gz9f"&gt;get_token&lt;/CODE&gt; directly at all.&lt;/LI&gt;
&lt;/UL&gt;
&lt;H3 class="_7uu25p0 qt3gz9c _7pq7t612 heading3 _7uu25p1"&gt;Quick checklist to fix your notebook&lt;/H3&gt;
&lt;UL class="qt3gz97 qt3gz92"&gt;
&lt;LI class="qt3gz9a"&gt;
&lt;P class="qt3gz91 paragraph"&gt;Decide on an auth method:&lt;/P&gt;
&lt;UL class="qt3gz98 qt3gz92"&gt;
&lt;LI class="qt3gz9a"&gt;
&lt;P class="qt3gz91 paragraph"&gt;Prefer UC &lt;STRONG&gt;Service Credential&lt;/STRONG&gt; and use &lt;CODE class="qt3gz9f"&gt;dbutils.credentials.getServiceCredentialsProvider("name")&lt;/CODE&gt; in code.&lt;/P&gt;
&lt;/LI&gt;
&lt;LI class="qt3gz9a"&gt;
&lt;P class="qt3gz91 paragraph"&gt;Or supply &lt;STRONG&gt;SP env vars&lt;/STRONG&gt; or &lt;STRONG&gt;managed identity&lt;/STRONG&gt; so DefaultAzureCredential has something to use.&lt;/P&gt;
&lt;/LI&gt;
&lt;/UL&gt;
&lt;/LI&gt;
&lt;LI class="qt3gz9a"&gt;
&lt;P class="qt3gz91 paragraph"&gt;Grant the chosen identity “Send” rights on the Service Bus topic/namespace.&lt;/P&gt;
&lt;/LI&gt;
&lt;LI class="qt3gz9a"&gt;
&lt;P class="qt3gz91 paragraph"&gt;Remove manual &lt;CODE class="qt3gz9f"&gt;get_token('xxx')&lt;/CODE&gt; call; let the Service Bus SDK obtain tokens using the credential you pass.&lt;/P&gt;
&lt;/LI&gt;
&lt;LI class="qt3gz9a"&gt;
&lt;P class="qt3gz91 paragraph"&gt;Keep the async pattern you have; it’s correct for &lt;CODE class="qt3gz9f"&gt;azure.servicebus.aio&lt;/CODE&gt;.&lt;/P&gt;
&lt;/LI&gt;
&lt;/UL&gt;</description>
    <pubDate>Tue, 18 Nov 2025 00:31:08 GMT</pubDate>
    <dc:creator>stbjelcevic</dc:creator>
    <dc:date>2025-11-18T00:31:08Z</dc:date>
    <item>
      <title>Adding a message to azure service bus</title>
      <link>https://community.databricks.com/t5/data-engineering/adding-a-message-to-azure-service-bus/m-p/91832#M38276</link>
      <description>&lt;P&gt;I am trying to send a message to a service bus in azure. But I get following error:&lt;BR /&gt;ServiceBusError: Handler failed: DefaultAzureCredential failed to retrieve a token from the included credentials.&lt;/P&gt;&lt;P&gt;This is the line that fails: credential = DefaultAzureCredential()&lt;/P&gt;&lt;P&gt;Anyone has a sample on how to add a message to a servicebus or similar?&lt;/P&gt;&lt;P&gt;Notebook:&lt;/P&gt;&lt;P&gt;import nest_asyncio&lt;BR /&gt;import asyncio&lt;BR /&gt;from azure.servicebus import ServiceBusMessage&lt;BR /&gt;from azure.servicebus.aio import ServiceBusClient&lt;/P&gt;&lt;P&gt;from azure.identity.aio import DefaultAzureCredential&lt;/P&gt;&lt;P&gt;nest_asyncio.apply()&lt;/P&gt;&lt;P&gt;local_user = dbutils.notebook.entry_point.getDbutils().notebook().getContext().userName().get()&lt;/P&gt;&lt;P&gt;FULLY_QUALIFIED_NAMESPACE = "xxx.servicebus.windows.net"&lt;BR /&gt;TOPIC_NAME = "xxoutbound"&lt;/P&gt;&lt;P&gt;credential = DefaultAzureCredential()&lt;BR /&gt;token = credential.get_token('xxx')&lt;BR /&gt;print(token)&lt;/P&gt;&lt;P&gt;async def send_single_message(sender):&lt;BR /&gt;message = ServiceBusMessage("Single Message")&lt;BR /&gt;await sender.send_messages(message)&lt;BR /&gt;print("Sent a single message")&lt;/P&gt;&lt;P&gt;async def run():&lt;BR /&gt;async with ServiceBusClient(&lt;BR /&gt;fully_qualified_namespace=FULLY_QUALIFIED_NAMESPACE,&lt;BR /&gt;credential=credential,&lt;BR /&gt;logging_enable=True) as servicebus_client:&lt;BR /&gt;sender = servicebus_client.get_topic_sender(topic_name=TOPIC_NAME)&lt;BR /&gt;async with sender:&lt;BR /&gt;await send_single_message(sender)&lt;/P&gt;&lt;P&gt;await credential.close()&lt;/P&gt;&lt;P&gt;asyncio.run(run())&lt;/P&gt;</description>
      <pubDate>Thu, 26 Sep 2024 08:50:41 GMT</pubDate>
      <guid>https://community.databricks.com/t5/data-engineering/adding-a-message-to-azure-service-bus/m-p/91832#M38276</guid>
      <dc:creator>Sega2</dc:creator>
      <dc:date>2024-09-26T08:50:41Z</dc:date>
    </item>
    <item>
      <title>Re: Adding a message to azure service bus</title>
      <link>https://community.databricks.com/t5/data-engineering/adding-a-message-to-azure-service-bus/m-p/108801#M43146</link>
      <description>&lt;P&gt;&lt;a href="https://community.databricks.com/t5/user/viewprofilepage/user-id/122912"&gt;@Sega2&lt;/a&gt;&amp;nbsp;- Can you explicitly useClientSecretCredential and try&lt;/P&gt;</description>
      <pubDate>Tue, 04 Feb 2025 14:27:57 GMT</pubDate>
      <guid>https://community.databricks.com/t5/data-engineering/adding-a-message-to-azure-service-bus/m-p/108801#M43146</guid>
      <dc:creator>Panda</dc:creator>
      <dc:date>2025-02-04T14:27:57Z</dc:date>
    </item>
    <item>
      <title>Re: Adding a message to azure service bus</title>
      <link>https://community.databricks.com/t5/data-engineering/adding-a-message-to-azure-service-bus/m-p/139454#M51205</link>
      <description>&lt;P class="qt3gz91 paragraph"&gt;It looks like the issue is with the Azure credential chain rather than Service Bus itself; in Databricks notebooks, DefaultAzureCredential won’t succeed unless there’s a valid identity available (env vars, CLI login, managed identity, or a Databricks “service credential” in Unity Catalog).&lt;/P&gt;
&lt;H3 class="_7uu25p0 qt3gz9c _7pq7t612 heading3 _7uu25p1"&gt;Why DefaultAzureCredential fails in Databricks&lt;/H3&gt;
&lt;UL class="qt3gz97 qt3gz92"&gt;
&lt;LI class="qt3gz9a"&gt;
&lt;P class="qt3gz91 paragraph"&gt;&lt;STRONG&gt;No Azure identity present&lt;/STRONG&gt; on the cluster: DefaultAzureCredential tries environment variables, then managed identity, then Azure CLI, etc.; without one configured, token acquisition fails immediately.&lt;/P&gt;
&lt;/LI&gt;
&lt;LI class="qt3gz9a"&gt;
&lt;P class="qt3gz91 paragraph"&gt;In Databricks, the recommended way to call Azure SDKs from notebooks is to use &lt;STRONG&gt;Unity Catalog Service Credentials&lt;/STRONG&gt; (which wrap a managed identity or service principal) and pass that to the Azure SDK client, rather than instantiating DefaultAzureCredential directly.&lt;/P&gt;
&lt;/LI&gt;
&lt;/UL&gt;
&lt;H3 class="_7uu25p0 qt3gz9c _7pq7t612 heading3 _7uu25p1"&gt;Two working approaches&lt;/H3&gt;
&lt;H4 class="_7uu25p0 qt3gz9c _7pq7t612 heading4 _7uu25p1"&gt;1) Use a Unity Catalog Service Credential (recommended)&lt;/H4&gt;
&lt;P class="qt3gz91 paragraph"&gt;This avoids storing secrets and works well from Databricks notebooks.&lt;/P&gt;
&lt;P class="qt3gz91 paragraph"&gt;High-level steps:&lt;/P&gt;
&lt;UL class="qt3gz97 qt3gz92"&gt;
&lt;LI class="qt3gz9a"&gt;
&lt;P class="qt3gz91 paragraph"&gt;Create a &lt;STRONG&gt;Service Credential&lt;/STRONG&gt; in Unity Catalog backed by an Azure managed identity via the Databricks Access Connector, and grant it rights to your Service Bus namespace/topic (Send permission on the topic or namespace).&lt;/P&gt;
&lt;/LI&gt;
&lt;LI class="qt3gz9a"&gt;
&lt;P class="qt3gz91 paragraph"&gt;In your notebook, retrieve the credential with &lt;CODE class="qt3gz9f"&gt;dbutils.credentials.getServiceCredentialsProvider('&amp;lt;service-credential-name&amp;gt;')&lt;/CODE&gt; and pass it to the Service Bus client.&lt;/P&gt;
&lt;/LI&gt;
&lt;/UL&gt;
&lt;P class="qt3gz91 paragraph"&gt;Example (async):&lt;/P&gt;
&lt;DIV class="go8b9g1 _7pq7t6cl" data-ui-element="code-block-container"&gt;
&lt;PRE&gt;&lt;CODE class="markdown-code-python qt3gz9e hljs language-python _1ymogdh2"&gt;%pip install azure-servicebus==&lt;SPAN class="hljs-number"&gt;7.12&lt;/SPAN&gt;&lt;SPAN class="hljs-number"&gt;.1&lt;/SPAN&gt; azure-identity==&lt;SPAN class="hljs-number"&gt;1.17&lt;/SPAN&gt;&lt;SPAN class="hljs-number"&gt;.0&lt;/SPAN&gt;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;DIV class="go8b9g3 _7pq7t62y _7pq7t6cm _7pq7t6ay _7pq7t6bo"&gt;
&lt;DIV class="go8b9g5 _7pq7t6cj"&gt;python&lt;/DIV&gt;
&lt;DIV class="_17yk06p0"&gt;&amp;nbsp;&lt;/DIV&gt;
&lt;/DIV&gt;
&lt;/DIV&gt;
&lt;DIV class="go8b9g1 _7pq7t6cl" data-ui-element="code-block-container"&gt;
&lt;PRE&gt;&lt;CODE class="markdown-code-python qt3gz9e hljs language-python _1ymogdh2"&gt;&lt;SPAN class="hljs-keyword"&gt;import&lt;/SPAN&gt; nest_asyncio, asyncio
nest_asyncio.apply()

&lt;SPAN class="hljs-keyword"&gt;from&lt;/SPAN&gt; azure.servicebus &lt;SPAN class="hljs-keyword"&gt;import&lt;/SPAN&gt; ServiceBusMessage
&lt;SPAN class="hljs-keyword"&gt;from&lt;/SPAN&gt; azure.servicebus.aio &lt;SPAN class="hljs-keyword"&gt;import&lt;/SPAN&gt; ServiceBusClient
&lt;SPAN class="hljs-comment"&gt;# IMPORTANT: use UC Service Credential provider, not DefaultAzureCredential&lt;/SPAN&gt;
credential = dbutils.credentials.getServiceCredentialsProvider(&lt;SPAN class="hljs-string"&gt;"my-service-credential"&lt;/SPAN&gt;)  &lt;SPAN class="hljs-comment"&gt;# name in UC&lt;/SPAN&gt;

FULLY_QUALIFIED_NAMESPACE = &lt;SPAN class="hljs-string"&gt;"xxx.servicebus.windows.net"&lt;/SPAN&gt;
TOPIC_NAME = &lt;SPAN class="hljs-string"&gt;"xxoutbound"&lt;/SPAN&gt;

&lt;SPAN class="hljs-keyword"&gt;async&lt;/SPAN&gt; &lt;SPAN class="hljs-keyword"&gt;def&lt;/SPAN&gt; &lt;SPAN class="hljs-title function_"&gt;send_single_message&lt;/SPAN&gt;(&lt;SPAN class="hljs-params"&gt;sender&lt;/SPAN&gt;):
    message = ServiceBusMessage(&lt;SPAN class="hljs-string"&gt;"Single Message"&lt;/SPAN&gt;)
    &lt;SPAN class="hljs-keyword"&gt;await&lt;/SPAN&gt; sender.send_messages(message)
    &lt;SPAN class="hljs-built_in"&gt;print&lt;/SPAN&gt;(&lt;SPAN class="hljs-string"&gt;"Sent a single message"&lt;/SPAN&gt;)

&lt;SPAN class="hljs-keyword"&gt;async&lt;/SPAN&gt; &lt;SPAN class="hljs-keyword"&gt;def&lt;/SPAN&gt; &lt;SPAN class="hljs-title function_"&gt;run&lt;/SPAN&gt;():
    &lt;SPAN class="hljs-keyword"&gt;async&lt;/SPAN&gt; &lt;SPAN class="hljs-keyword"&gt;with&lt;/SPAN&gt; ServiceBusClient(
        fully_qualified_namespace=FULLY_QUALIFIED_NAMESPACE,
        credential=credential,
        logging_enable=&lt;SPAN class="hljs-literal"&gt;True&lt;/SPAN&gt;,
    ) &lt;SPAN class="hljs-keyword"&gt;as&lt;/SPAN&gt; servicebus_client:
        sender = servicebus_client.get_topic_sender(topic_name=TOPIC_NAME)
        &lt;SPAN class="hljs-keyword"&gt;async&lt;/SPAN&gt; &lt;SPAN class="hljs-keyword"&gt;with&lt;/SPAN&gt; sender:
            &lt;SPAN class="hljs-keyword"&gt;await&lt;/SPAN&gt; send_single_message(sender)

asyncio.run(run())&lt;/CODE&gt;&lt;/PRE&gt;
&lt;/DIV&gt;
&lt;P class="qt3gz91 paragraph"&gt;Notes:&lt;/P&gt;
&lt;UL class="qt3gz97 qt3gz92"&gt;
&lt;LI class="qt3gz9a"&gt;
&lt;P class="qt3gz91 paragraph"&gt;The UC service credential object must have permission to send to the topic; assign RBAC (e.g., Azure Service Bus “Data Sender” role) to the managed identity attached to the Access Connector on the Service Bus resource.&lt;/P&gt;
&lt;/LI&gt;
&lt;LI class="qt3gz9a"&gt;
&lt;P class="qt3gz91 paragraph"&gt;If you prefer setting a default service credential for the cluster, you can set &lt;CODE class="qt3gz9f"&gt;DATABRICKS_DEFAULT_SERVICE_CREDENTIAL_NAME&lt;/CODE&gt; as an environment variable on the cluster and then use SDK defaults, but naming it explicitly in code is more portable.&lt;/P&gt;
&lt;/LI&gt;
&lt;/UL&gt;
&lt;H4 class="_7uu25p0 qt3gz9c _7pq7t612 heading4 _7uu25p1"&gt;2) Use DefaultAzureCredential with an actual identity source&lt;/H4&gt;
&lt;P class="qt3gz91 paragraph"&gt;If you still want DefaultAzureCredential, ensure one of its sources is available on the cluster:&lt;/P&gt;
&lt;P class="qt3gz91 paragraph"&gt;Options:&lt;/P&gt;
&lt;UL class="qt3gz97 qt3gz92"&gt;
&lt;LI class="qt3gz9a"&gt;
&lt;P class="qt3gz91 paragraph"&gt;Configure &lt;STRONG&gt;environment variables&lt;/STRONG&gt; for a service principal: &lt;CODE class="qt3gz9f"&gt;AZURE_CLIENT_ID&lt;/CODE&gt;, &lt;CODE class="qt3gz9f"&gt;AZURE_TENANT_ID&lt;/CODE&gt;, &lt;CODE class="qt3gz9f"&gt;AZURE_CLIENT_SECRET&lt;/CODE&gt; on the cluster, and grant that SP sender permissions on Service Bus.&lt;/P&gt;
&lt;/LI&gt;
&lt;LI class="qt3gz9a"&gt;
&lt;P class="qt3gz91 paragraph"&gt;Attach a &lt;STRONG&gt;managed identity&lt;/STRONG&gt; to the Databricks Access Connector and let DefaultAzureCredential pick it up if you also set it as the default service credential for the cluster. In Databricks classic compute, you can set &lt;CODE class="qt3gz9f"&gt;DATABRICKS_DEFAULT_SERVICE_CREDENTIAL_NAME&lt;/CODE&gt;, then simply use the Azure SDK without passing a credential explicitly.&lt;/P&gt;
&lt;/LI&gt;
&lt;/UL&gt;
&lt;P class="qt3gz91 paragraph"&gt;Example (DefaultAzureCredential after setting identity):&lt;/P&gt;
&lt;DIV class="go8b9g1 _7pq7t6cl" data-ui-element="code-block-container"&gt;
&lt;PRE&gt;&lt;CODE class="markdown-code-python qt3gz9e hljs language-python _1ymogdh2"&gt;&lt;SPAN class="hljs-keyword"&gt;from&lt;/SPAN&gt; azure.identity.aio &lt;SPAN class="hljs-keyword"&gt;import&lt;/SPAN&gt; DefaultAzureCredential
&lt;SPAN class="hljs-keyword"&gt;from&lt;/SPAN&gt; azure.servicebus.aio &lt;SPAN class="hljs-keyword"&gt;import&lt;/SPAN&gt; ServiceBusClient
&lt;SPAN class="hljs-keyword"&gt;from&lt;/SPAN&gt; azure.servicebus &lt;SPAN class="hljs-keyword"&gt;import&lt;/SPAN&gt; ServiceBusMessage
&lt;SPAN class="hljs-keyword"&gt;import&lt;/SPAN&gt; asyncio, nest_asyncio
nest_asyncio.apply()

FULLY_QUALIFIED_NAMESPACE = &lt;SPAN class="hljs-string"&gt;"xxx.servicebus.windows.net"&lt;/SPAN&gt;
TOPIC_NAME = &lt;SPAN class="hljs-string"&gt;"xxoutbound"&lt;/SPAN&gt;

credential = DefaultAzureCredential()

&lt;SPAN class="hljs-keyword"&gt;async&lt;/SPAN&gt; &lt;SPAN class="hljs-keyword"&gt;def&lt;/SPAN&gt; &lt;SPAN class="hljs-title function_"&gt;run&lt;/SPAN&gt;():
    &lt;SPAN class="hljs-keyword"&gt;async&lt;/SPAN&gt; &lt;SPAN class="hljs-keyword"&gt;with&lt;/SPAN&gt; ServiceBusClient(FULLY_QUALIFIED_NAMESPACE, credential=credential, logging_enable=&lt;SPAN class="hljs-literal"&gt;True&lt;/SPAN&gt;) &lt;SPAN class="hljs-keyword"&gt;as&lt;/SPAN&gt; sb:
        sender = sb.get_topic_sender(topic_name=TOPIC_NAME)
        &lt;SPAN class="hljs-keyword"&gt;async&lt;/SPAN&gt; &lt;SPAN class="hljs-keyword"&gt;with&lt;/SPAN&gt; sender:
            &lt;SPAN class="hljs-keyword"&gt;await&lt;/SPAN&gt; sender.send_messages(ServiceBusMessage(&lt;SPAN class="hljs-string"&gt;"hello"&lt;/SPAN&gt;))

    &lt;SPAN class="hljs-keyword"&gt;await&lt;/SPAN&gt; credential.close()

asyncio.run(run())&lt;/CODE&gt;&lt;/PRE&gt;
&lt;/DIV&gt;
&lt;P class="qt3gz91 paragraph"&gt;Common pitfall in your snippet:&lt;/P&gt;
&lt;UL class="qt3gz97 qt3gz92"&gt;
&lt;LI class="qt3gz9a"&gt;&lt;CODE class="qt3gz9f"&gt;get_token('xxx')&lt;/CODE&gt; is not a valid scope for Azure AD; when testing, use a real scope (for Service Bus you typically let the client fetch tokens internally, you don’t need to call &lt;CODE class="qt3gz9f"&gt;get_token&lt;/CODE&gt; yourself). If you do test, use a resource scope like &lt;CODE class="qt3gz9f"&gt;"&lt;A href="https://servicebus.azure.net/.default" target="_blank"&gt;https://servicebus.azure.net/.default&lt;/A&gt;"&lt;/CODE&gt; or the Azure SDK will handle it automatically when you send messages. But with UC service credentials, you usually don’t call &lt;CODE class="qt3gz9f"&gt;get_token&lt;/CODE&gt; directly at all.&lt;/LI&gt;
&lt;/UL&gt;
&lt;H3 class="_7uu25p0 qt3gz9c _7pq7t612 heading3 _7uu25p1"&gt;Quick checklist to fix your notebook&lt;/H3&gt;
&lt;UL class="qt3gz97 qt3gz92"&gt;
&lt;LI class="qt3gz9a"&gt;
&lt;P class="qt3gz91 paragraph"&gt;Decide on an auth method:&lt;/P&gt;
&lt;UL class="qt3gz98 qt3gz92"&gt;
&lt;LI class="qt3gz9a"&gt;
&lt;P class="qt3gz91 paragraph"&gt;Prefer UC &lt;STRONG&gt;Service Credential&lt;/STRONG&gt; and use &lt;CODE class="qt3gz9f"&gt;dbutils.credentials.getServiceCredentialsProvider("name")&lt;/CODE&gt; in code.&lt;/P&gt;
&lt;/LI&gt;
&lt;LI class="qt3gz9a"&gt;
&lt;P class="qt3gz91 paragraph"&gt;Or supply &lt;STRONG&gt;SP env vars&lt;/STRONG&gt; or &lt;STRONG&gt;managed identity&lt;/STRONG&gt; so DefaultAzureCredential has something to use.&lt;/P&gt;
&lt;/LI&gt;
&lt;/UL&gt;
&lt;/LI&gt;
&lt;LI class="qt3gz9a"&gt;
&lt;P class="qt3gz91 paragraph"&gt;Grant the chosen identity “Send” rights on the Service Bus topic/namespace.&lt;/P&gt;
&lt;/LI&gt;
&lt;LI class="qt3gz9a"&gt;
&lt;P class="qt3gz91 paragraph"&gt;Remove manual &lt;CODE class="qt3gz9f"&gt;get_token('xxx')&lt;/CODE&gt; call; let the Service Bus SDK obtain tokens using the credential you pass.&lt;/P&gt;
&lt;/LI&gt;
&lt;LI class="qt3gz9a"&gt;
&lt;P class="qt3gz91 paragraph"&gt;Keep the async pattern you have; it’s correct for &lt;CODE class="qt3gz9f"&gt;azure.servicebus.aio&lt;/CODE&gt;.&lt;/P&gt;
&lt;/LI&gt;
&lt;/UL&gt;</description>
      <pubDate>Tue, 18 Nov 2025 00:31:08 GMT</pubDate>
      <guid>https://community.databricks.com/t5/data-engineering/adding-a-message-to-azure-service-bus/m-p/139454#M51205</guid>
      <dc:creator>stbjelcevic</dc:creator>
      <dc:date>2025-11-18T00:31:08Z</dc:date>
    </item>
  </channel>
</rss>

