错误:使用正确的连接字符串打开服务客户端

huangapple go评论76阅读模式
英文:

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 &quot;main&quot; 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 = &quot;Endpoint=sb://[example_endpoint].servicebus.windows.net/;SharedAccessKeyName=[mySharedAccessName];SharedAccessKey=[SharedAccessKey]&quot;;
    private static final String deviceId = &quot;my_device_example&quot;;

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 &quot;1.8.0_265&quot;
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)
        &lt;dependency&gt;
            &lt;groupId&gt;com.microsoft.azure.sdk.iot&lt;/groupId&gt;
            &lt;artifactId&gt;iot-service-client&lt;/artifactId&gt;
            &lt;version&gt;1.24.0&lt;/version&gt;
        &lt;/dependency&gt;

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 -&gt; Shared access policies -&gt; &lt;select policy name&gt; -&gt; Connection string—primary key

huangapple
  • 本文由 发表于 2020年10月4日 03:07:03
  • 转载请务必保留本文链接:https://go.coder-hub.com/64187990.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定