Error: opening Service Client using correct connection string

1.4k views Asked by At

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

There are 1 answers

2
iAviator On

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