Build issue with Java passwordless connections to Azure Cosmos DB

91 Views Asked by At

I am trying to build an Azure client using Java passwordless connections to Azure Cosmos DB for NoSQL. I have followed the instructions provided in this Microsoft Learn article and created a custom role as recommended. However, I am receiving the following error:

Exception in thread "main" java.lang.RuntimeException: Client initialization failed. Check if the endpoint is reachable and if your auth token is valid. More info: https://aka.ms/cosmosdb-tsg-service-unavailable-java
    at com.azure.cosmos.implementation.RxDocumentClientImpl.initializeGatewayConfigurationReader(RxDocumentClientImpl.java:512)
    at com.azure.cosmos.implementation.RxDocumentClientImpl.init(RxDocumentClientImpl.java:547)
    at com.azure.cosmos.implementation.AsyncDocumentClient$Builder.build(AsyncDocumentClient.java:295)
    at com.azure.cosmos.CosmosAsyncClient.<init>(CosmosAsyncClient.java:171)
    at com.azure.cosmos.CosmosClientBuilder.buildAsyncClient(CosmosClientBuilder.java:1052)
    at com.azure.cosmos.CosmosClient.<init>(CosmosClient.java:38)
    at com.azure.cosmos.CosmosClientBuilder.buildClient(CosmosClientBuilder.java:1086)
    at com.azure.cosmos.sample.sync.CosmoDBClientPasswordless.main(CosmoDBClientPasswordless.java:25)

Here is the code I am using:

public class CosmoDBClientPasswordless {  
  public static void main(String[] args) {   
     // Set up your Cosmos DB client   
     String endpoint = "<endpoint>";
     DefaultAzureCredential credential = new DefaultAzureCredentialBuilder()  
.build();   
     CosmosClient client = new CosmosClientBuilder().endpoint(endpoint)
                           .credential(credential)
                           .consistencyLevel(ConsistencyLevel.EVENTUAL)
                           .buildClient();  
       
}

}

I have already set my env variables AZURE_SUBSCRIPTION_ID AZURE_CLIENT_ID AZURE_CLIENT_SECRET AZURE_TENANT_ID

I have validated these keys and with same keys I am able to access Azure Blob Storage.

Created custom role as suggested in doc, It worked once but now its not working.

My code though if I use cosmos secret key and same endpoint like following it works.

 String endpoint = "<endpoint>";
        String key = "<key>";
        CosmosClientBuilder clientBuilder = new CosmosClientBuilder()
                .endpoint(endpoint)
                .key(key);

        CosmosClient client = clientBuilder.buildClient();
0

There are 0 best solutions below