Build issue with Java passwordless connections to Azure Cosmos DB

Kushv 0 Reputation points
2023-10-07T10:19:52.2333333+00:00

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();  
       
}
}

What could be causing this issue, and how can I resolve it?

Azure Cosmos DB
Azure Cosmos DB
An Azure NoSQL database service for app development.
1,839 questions
{count} votes

1 answer

Sort by: Most helpful
  1. ShaktiSingh-MSFT 16,271 Reputation points
    2023-10-09T05:42:55.93+00:00

    Hi
    Kushv
    •,

    Welcome to Microsoft Q&A forum and thanks for using Azure Services.

    As I understand, you are getting error "Client initialization failed. Check if the endpoint is reachable and if your auth token is valid." when Migrate an application to use passwordless connections with Azure Cosmos DB for NoSQL.

    Could you please make sure if the role is correct?

    When developing locally with passwordless authentication, make sure the user account that connects to Cosmos DB is assigned a role with the correct permissions to perform data operations. Currently, Azure Cosmos DB for NoSQL doesn't include built-in roles for data operations, but you can create your own using the Azure CLI or PowerShell.

    Roles consist of a collection of permissions or actions that a user is allowed to perform, such as read, write, and delete. You can read more about configuring role-based access control (RBAC) in the Cosmos DB security configuration documentation.

    Refer: Create the custom role

    Please check and let us know.

    Thanks

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.