我正在尝试直接使用MQTT连接到IOT中心。
我只是遵循以下文档,
https://docs.microsoft.com/en-us/azure/iot-central/concepts-connectivity#connect-a-single-device
https://www.instructables.com/id/Azure-IoT-Hub-Set-Up-MQTTfx-Sigfox-Callback-and-Dr/
https://github.com/MediaTek-Labs/aws_mbedtls_mqtt/pull/9/files?short_path=04c6e90
https://docs.azure.cn/zh-cn/articles/azure-operations-guide/iot-hub/aog-iot-hub-howto-connect-with-tool-mqtt-fx(翻译成英文)
在上述文档中,我使用了来自github的dps_cstr工具,通过此链接使用SAS令牌生成了连接字符串。
https://docs.microsoft.com/en-us/azure/iot-central/tutorial-add-device#prepare-the-client-code
经纪人地址= saas-iothub-947867dc-cd5d-446c-90ff-e0f964f020fe.azure-devices.net
代理端口= 8883
客户端ID = 92ff3e25-00e5-4249-9074
用户名= saas-iothub-947867dc-cd5d-446c-90ff.azure-devices.net/92ff3e25-00e5-4249-9074
MQTT版本= 3.1.1
密码= OfAlY0BGstmuinZzOcdDDf
我已经通过使用上面的连接字符串和详细信息配置了带有iot Central的连接配置文件的MQTT.fx。
附上那个
当我尝试连接到此计算机时,出现错误“未授权连接”。
日志:
2018-12-20 00:42:49,738信息--- BrokerConnectorController:onConnect
2018-12-20 00:42:49,740信息--ScriptsController:清除控制台。
2018-12-20 00:42:49,804信息--- MqttFX ClientModel:已分配ID 92ff3e25-00e5-4249-9074-854b43b5a949的MqttClient。
2018-12-20 00:42:53,571错误--- MqttFX ClientModel:连接时出错
org.eclipse.paho.client.mqttv3.MqttSecurityException:未经授权连接
在org.eclipse.paho.client.mqttv3.internal.ExceptionHelper.createMqttException(ExceptionHelper.java:28)〜[org.eclipse.paho.client.mqttv3-1.2.0.jar :?]
在org.eclipse.paho.client.mqttv3.internal.ClientState.notifyReceivedAck(ClientState.java:988)〜[org.eclipse.paho.client.mqttv3-1.2.0.jar :?]
在org.eclipse.paho.client.mqttv3.internal.CommsReceiver.run(CommsReceiver.java:145)〜[org.eclipse.paho.client.mqttv3-1.2.0.jar :?]
在java.util.concurrent.Executors $ RunnableAdapter.call(未知来源)〜[?:1.8.0_181]
在java.util.concurrent.FutureTask.run(未知来源)〜[?:1.8.0_181]
在java.util.concurrent.ScheduledThreadPoolExecutor $ ScheduledFutureTask.access $ 201(未知来源)〜[?:1.8.0_181]
在java.util.concurrent.ScheduledThreadPoolExecutor $ ScheduledFutureTask.run(未知来源)〜[?:1.8.0_181]
在java.util.concurrent.ThreadPoolExecutor.runWorker(未知来源)[?: 1.8.0_181]
在java.util.concurrent.ThreadPoolExecutor $ Worker.run(未知来源)[?: 1.8.0_181]
在java.lang.Thread.run(未知来源)[?: 1.8.0_181]
2018-12-20 00:42:53,572错误--- MqttFX ClientModel:请验证您的设置(例如经纪人地址,经纪人端口和客户ID)和用户凭证!
org.eclipse.paho.client.mqttv3.MqttSecurityException:未经授权连接
在org.eclipse.paho.client.mqttv3.internal.ExceptionHelper.createMqttException(ExceptionHelper.java:28)〜[org.eclipse.paho.client.mqttv3-1.2.0.jar :?]
在org.eclipse.paho.client.mqttv3.internal.ClientState.notifyReceivedAck(ClientState.java:988)〜[org.eclipse.paho.client.mqttv3-1.2.0.jar :?]
在org.eclipse.paho.client.mqttv3.internal.CommsReceiver.run(CommsReceiver.java:145)〜[org.eclipse.paho.client.mqttv3-1.2.0.jar :?]
在java.util.concurrent.Executors $ RunnableAdapter.call(未知来源)〜[?:1.8.0_181]
在java.util.concurrent.FutureTask.run(未知来源)〜[?:1.8.0_181]
在java.util.concurrent.ScheduledThreadPoolExecutor $ ScheduledFutureTask.access $ 201(未知来源)〜[?:1.8.0_181]
在java.util.concurrent.ScheduledThreadPoolExecutor $ ScheduledFutureTask.run(未知来源)〜[?:1.8.0_181]
在java.util.concurrent.ThreadPoolExecutor.runWorker(未知来源)[?: 1.8.0_181]
在java.util.concurrent.ThreadPoolExecutor $ Worker.run(未知来源)[?: 1.8.0_181]
在java.lang.Thread.run(未知来源)[?: 1.8.0_181]
2018-12-20 00:42:53,574信息--- ScriptsController:清除控制台。
2018-12-20 00:42:53,574错误--- BrokerConnectService:未经授权连接
编辑:
我从https://github.com/Azure/azure-iot-sdk-c/blob/master/certs/certs.c创建了一个CA证书,名为CACert.cer
我在SSL / TLS选项中选择了CS证书文件选项,然后重试。 但是我有同样的错误
答案 0 :(得分:2)
我刚刚使用连接到Azure IoT中心的MQTT.fx客户端完成了测试。 根据文档Using the MQTT protocol directly,密码必须采用以下格式,请参见示例:
SharedAccessSignature sr={your hub name}.azure-devices.net%2Fdevices%2FMyDevice01%2Fapi-version%3D2016-11-14&sig=vSgHBMUG.....Ntg%3d&se=1456481802
您必须生成上述密码(sas令牌)字符串。您可以使用以下帮助器功能:
string sasToken = SharedAccessSignatureBuilder.GetSASTokenFromConnectionString(connectionString);
public sealed class SharedAccessSignatureBuilder
{
public static string GetHostNameNamespaceFromConnectionString(string connectionString)
{
return GetPartsFromConnectionString(connectionString)["HostName"].Split('.').FirstOrDefault();
}
public static string GetSASTokenFromConnectionString(string connectionString, uint hours = 24)
{
var parts = GetPartsFromConnectionString(connectionString);
return GetSASToken(parts["HostName"], parts["SharedAccessKey"], parts.Keys.Contains("SharedAccessKeyName") ? parts["SharedAccessKeyName"] : null, hours);
}
public static string GetSASToken(string resourceUri, string key, string keyName = null, uint hours = 24)
{
var expiry = GetExpiry(hours);
string stringToSign = System.Web.HttpUtility.UrlEncode(resourceUri) + "\n" + expiry;
HMACSHA256 hmac = new HMACSHA256(Convert.FromBase64String(key));
var signature = Convert.ToBase64String(hmac.ComputeHash(Encoding.UTF8.GetBytes(stringToSign)));
var sasToken = String.Format(CultureInfo.InvariantCulture, "SharedAccessSignature sr={0}&sig={1}&se={2}", HttpUtility.UrlEncode(resourceUri), HttpUtility.UrlEncode(signature), expiry);
if(!string.IsNullOrEmpty(keyName))
sasToken += String.Format(CultureInfo.InvariantCulture, "&skn={0}", keyName);
return sasToken;
}
#region Helpers
private static Dictionary<string, string> GetPartsFromConnectionString(string connectionString)
{
return connectionString.Split(new[] { ';' }, StringSplitOptions.RemoveEmptyEntries).Select(s => s.Split(new[] { '=' }, 2)).ToDictionary(x => x[0].Trim(), x => x[1].Trim());
}
// default expiring = 24 hours
private static string GetExpiry(uint hours = 24)
{
TimeSpan sinceEpoch = DateTime.UtcNow - new DateTime(1970, 1, 1);
return Convert.ToString((int)sinceEpoch.TotalSeconds + 3600 * hours);
}
#endregion
}
以下屏幕片段显示了连接到Azure IoT中心和设备仪表板的MQTT.fx设备(myfirstdevice)。