英文:
Error: opening Service Client using correct connection string
问题
我正试图运行 Service Client 示例 https://github.com/Azure/azure-iot-sdk-java/blob/master/service/iot-service-samples/service-client-sample/src/main/java/samples/com/microsoft/azure/sdk/iot/ServiceClientSample.java,并且使用正确的连接字符串,但是它返回了一个错误:
********* 开始 ServiceClient 示例...
正在创建 ServiceClient...
Exception in thread "main" java.lang.IllegalArgumentException: 连接字符串的属性具有无效值。
at com.microsoft.azure.sdk.iot.service.IotHubConnectionStringBuilder.validateFormat(IotHubConnectionStringBuilder.java:196)
at com.microsoft.azure.sdk.iot.service.IotHubConnectionStringBuilder.validate(IotHubConnectionStringBuilder.java:176)
at com.microsoft.azure.sdk.iot.service.IotHubConnectionStringBuilder.parse(IotHubConnectionStringBuilder.java:132)
at com.microsoft.azure.sdk.iot.service.IotHubConnectionStringBuilder.createConnectionString(IotHubConnectionStringBuilder.java:42)
at com.microsoft.azure.sdk.iot.service.ServiceClient.createFromConnectionString(ServiceClient.java:69)
at com.microsoft.azure.sdk.iot.service.ServiceClient.createFromConnectionString(ServiceClient.java:44)
at testiothub.Main.openServiceClient(Main.java:104)
at testiothub.Main.main(Main.java:52)
我正在使用以下内容:
private static final String connectionString = "Endpoint=sb://[example_endpoint].servicebus.windows.net/;SharedAccessKeyName=[mySharedAccessName];SharedAccessKey=[SharedAccessKey]";
private static final String deviceId = "my_device_example";
其中 example_endpoint
是我的终结点,mySharedAccessName
是我的共享访问名称,SharedAccessKey
是我的密钥。
我认为这是一个基于 Java 正则表达式的错误,位于第 196 行:https://github.com/Azure/azure-iot-sdk-java/blob/94930586487573a9f9d43cf10fe6b17ae71c01c3/service/iot-service-client/src/main/java/com/microsoft/azure/sdk/iot/service/IotHubConnectionStringBuilder.java#L196
我的 Java 版本和 Azure SDK 版本如下:
openjdk version "1.8.0_265"
OpenJDK Runtime Environment Corretto-8.265.01.1 (build 1.8.0_265-b01)
OpenJDK 64-Bit Server VM Corretto-8.265.01.1 (build 25.265-b01, mixed mode)
<dependency>
<groupId>com.microsoft.azure.sdk.iot</groupId>
<artifactId>iot-service-client</artifactId>
<version>1.24.0</version>
</dependency>
我该如何解决这个问题?
英文:
I am trying to run Service Client Sample example https://github.com/Azure/azure-iot-sdk-java/blob/master/service/iot-service-samples/service-client-sample/src/main/java/samples/com/microsoft/azure/sdk/iot/ServiceClientSample.java with correct connection string, but it returns an error:
********* Starting ServiceClient sample...
Creating ServiceClient...
Exception in thread "main" java.lang.IllegalArgumentException: The connection string has an invalid value for property.
at com.microsoft.azure.sdk.iot.service.IotHubConnectionStringBuilder.validateFormat(IotHubConnectionStringBuilder.java:196)
at com.microsoft.azure.sdk.iot.service.IotHubConnectionStringBuilder.validate(IotHubConnectionStringBuilder.java:176)
at com.microsoft.azure.sdk.iot.service.IotHubConnectionStringBuilder.parse(IotHubConnectionStringBuilder.java:132)
at com.microsoft.azure.sdk.iot.service.IotHubConnectionStringBuilder.createConnectionString(IotHubConnectionStringBuilder.java:42)
at com.microsoft.azure.sdk.iot.service.ServiceClient.createFromConnectionString(ServiceClient.java:69)
at com.microsoft.azure.sdk.iot.service.ServiceClient.createFromConnectionString(ServiceClient.java:44)
at testiothub.Main.openServiceClient(Main.java:104)
at testiothub.Main.main(Main.java:52)
I am using:
private static final String connectionString = "Endpoint=sb://[example_endpoint].servicebus.windows.net/;SharedAccessKeyName=[mySharedAccessName];SharedAccessKey=[SharedAccessKey]";
private static final String deviceId = "my_device_example";
Where example_endpoint
is my endpoint and mySharedAccessName
is the name of my shared access name and SharedAccessKey
is my key.
I think it is an error based on Java regex located in line 196: https://github.com/Azure/azure-iot-sdk-java/blob/94930586487573a9f9d43cf10fe6b17ae71c01c3/service/iot-service-client/src/main/java/com/microsoft/azure/sdk/iot/service/IotHubConnectionStringBuilder.java#L196
My java version and Azure SDK is:
openjdk version "1.8.0_265"
OpenJDK Runtime Environment Corretto-8.265.01.1 (build 1.8.0_265-b01)
OpenJDK 64-Bit Server VM Corretto-8.265.01.1 (build 25.265-b01, mixed mode)
<dependency>
<groupId>com.microsoft.azure.sdk.iot</groupId>
<artifactId>iot-service-client</artifactId>
<version>1.24.0</version>
</dependency>
How can I solve this problem?
答案1
得分: 2
显示的错误是正确的。您正在使用以 sb://
开头的Event Hub兼容端点
的连接字符串,即它使用了service bus。如果您想使用serviceClient
,您需要使用IoT hub
的连接字符串,可以在IoT hub
资源的概述
部分找到,然后依次点击共享访问策略
,选择相应的策略名称,然后点击连接字符串 - 主键
。
英文:
The error shown is correct. The connection string you are using is of Event Hub-compatible endpoint
which starts with sb://
i.e it uses service bus.
If you want to use the serviceClient you need to use the connection string of the iotHub and can be found in IoT hub blade -> Shared access policies -> <select policy name> -> Connection string—primary key
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论