【问题标题】:How do I add Send permission to my IoT Hub connection string?如何将发送权限添加到我的 IoT 中心连接字符串?
【发布时间】:2018-09-23 04:12:01
【问题描述】:

我正在创建一个需要向 Azure IoT 中心发送消息的 Azure 函数。当函数尝试发送时,我收到以下错误:

2018-04-12T20:19:28.567 [Error] Microsoft.ServiceBus: Unauthorized access. 'Send' claim(s) are required to perform this operation. Resource: 'sb://iothub-ns-monitorhub-xxxxx-xxxxxxxx.servicebus.windows.net/monitorhub'. TrackingId:a79b2055d85446bd9739bd229aada0d7_G1, SystemTracker:gateway5 

所以看来我需要向我的集线器连接字符串添加一个发送声明。但是,当我转到集线器的命名空间并单击共享访问策略时,我会看到:

权限列表不包括“发送”权限。如何将发送权限添加到集线器的连接字符串?

以下是发生错误的 Azure 函数。代码编译没有错误,但会产生运行时错误。

#r "Microsoft.ServiceBus"

using System;
using System.Text;
using Microsoft.ServiceBus.Messaging;

public static void Run(EventData eventData, out string outputEventHubMessage, TraceWriter log)
{
    // Get some system properties from the SystemProperties dictionary
    var deviceId = eventData.SystemProperties["iothub-connection-device-id"].ToString();
    var messageSource = eventData.SystemProperties["iothub-message-source"].ToString();
    var enqueuedTime = eventData.SystemProperties["iothub-enqueuedtime"].ToString();
    var sequenceNumber = eventData.SystemProperties["SequenceNumber"].ToString();
    var offset = eventData.SystemProperties["Offset"].ToString();

    var data = Encoding.UTF8.GetString(eventData.GetBytes());
    var message = string.Format("Message Source: {0}; Enqueued Time: {1}; Sequence Number: {2}; Offset: {3}; DeviceId: {4}; Data: {5}", messageSource, enqueuedTime, sequenceNumber, offset, deviceId, data);

    outputEventHubMessage = message;

    log.Info($"{message}");
}

【问题讨论】:

  • 没有任何代码很难说发生了什么。请考虑编辑您的问题以添加发送消息的代码 sn-p?
  • @pierreca-MSFT - 我已经这样做了。感谢您的协助。
  • 对不起,我不完全确定您尝试将数据发送到哪个端点:是否发送到单独的事件中心(在这种情况下,您可能需要使用此 EH 连接字符串) 或返回 IoT 中心(在这种情况下,您是否尝试发送到 D2C 或 C2D 端点?我不确定 azure 函数输出绑定是否支持)
  • @pierreca-MSFT - 我正在尝试将其发送到不同的物联网集线器。你说,“这个 EH 连接字符串”。你指的是什么?还有,感谢您的帮助!
  • 好的,我不确定 azure 函数事件中心输出绑定是否允许将数据发送到 IoT 中心,但您可以使用函数代码中的 SDK 来实现。您尝试将数据发送到 IoT 中心的哪个端点? (有关 IoT 中心端点的入门知识,请参阅 docs.microsoft.com/en-us/azure/iot-hub/…)。

标签: azure-functions azure-iot-hub


【解决方案1】:

根据您更新的代码和 cmets,您希望将事件发送到内置于事件中心兼容端点的 azure iot 中心。但它只能用于接收 D2C 消息。

IoT 中心为您的后端服务公开消息/事件内置终结点,以读取您的中心接收到的设备到云消息。此端点与事件中心兼容,使您能够使用事件中心服务支持的任何机制来读取消息。

参考"Read device-to-cloud messages from the built-in endpoint"

Azure Function 通常由来自 azure iot hub 的设备消息触发,并且可以将这些消息路由到 Azure 存储或采取其他操作。

如果您仍想从 azure 功能发送 D2C 消息或有任何其他问题,请随时告诉我。

一些参考资料:

Processing data from IoT Hub with Azure Functions

Send device-to-cloud messages to IoT Hub

Send simulated telemetry

【讨论】:

  • Rita - 你可以从我的屏幕截图中看到“iothubowner”已经拥有 DeviceConnect 权限。
猜你喜欢
  • 2015-07-02
  • 1970-01-01
  • 2016-11-29
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多