Databricks 上的托管身份 - DefaultAzureCredential 无法从包含的凭据中检索令牌

问题描述 投票:0回答:1

我正在尝试向 azure 中的服务总线发送消息。

但是我收到以下错误:

 ServiceBusError: Handler failed: DefaultAzureCredential failed to
 retrieve a token from the included credentials.

这是失败的行:

credential = DefaultAzureCredential()

通常,我会使用 az 登录,但不确定如何在 databricks 中执行此操作。

import nest_asyncio
import asyncio
from azure.servicebus import ServiceBusMessage
from azure.servicebus.aio import ServiceBusClient

from azure.identity.aio import DefaultAzureCredential

nest_asyncio.apply()

local_user = dbutils.notebook.entry_point.getDbutils().notebook().getContext().userName().get()

print(local_user)

FULLY_QUALIFIED_NAMESPACE = "xxx.servicebus.windows.net"
TOPIC_NAME = "xxoutbound"

credential = DefaultAzureCredential()
token = credential.get_token('xxx')
print(token)

async def send_single_message(sender):
    # Create a Service Bus message and send it to the queue
    message = ServiceBusMessage("Single Message")
    await sender.send_messages(message)
    print("Sent a single message")

async def run():
    # create a Service Bus client using the credential
    async with ServiceBusClient(
        fully_qualified_namespace=FULLY_QUALIFIED_NAMESPACE,
        credential=credential,
        logging_enable=True) as servicebus_client:
        # get a Queue Sender object to send messages to the queue
        sender = servicebus_client.get_topic_sender(topic_name=TOPIC_NAME)
        async with sender:
            # send one message
            await send_single_message(sender)

        # Close credential when no longer needed.
        await credential.close()

print("dfsdf")

asyncio.run(run())
print("Done sending messages")
print("-----------------------")
python databricks azure-databricks azureservicebus
1个回答
0
投票

您无需检索令牌,只需将凭证传递给客户端并发送消息即可。

当您使用默认凭据时,它将使用托管身份作为授权凭据。因此,您需要为该身份授予Azure Service Bus Data Sender角色。

enter image description here

您可以在 databricks 托管资源组中看到身份资源。

输出:

在dbx中 enter image description here

以及正在服务的巴士队列

enter image description here

© www.soinside.com 2019 - 2024. All rights reserved.