0

I am trying to connect to cloudantDB from local. This is the code that I get from the IBM example and it is working on deployed version online.

   try {
        System.out.println("Connecting to Cloudant : " + user);

        //This works
        CloudantClient client = ClientBuilder.account(user)
                .username(user)
                .password(password)
                .connectTimeout(30, TimeUnit.SECONDS)
                .readTimeout(30, TimeUnit.SECONDS)
                .build();
        System.out.println("Connected to Cloudant : " + user);

        //This doesn't works and fire the errror Show the server version
        System.out.println("Server Version: " + client.serverVersion());

        //This doesn't works and fire the error to. Show databases
        System.out.println("Dbs: " + client.getAllDbs());

        return client;
    } catch (Exception e) {
        throw new RuntimeException("Unable to connect", e);
    }

And from localhost I have this error:

[ERROR   ] Failed to get cookie from server, response code 503, cookie authentication will not be attempted again 
[err] java.lang.RuntimeException: Unable to connect to repository
[err]   at it.sinesy.nosql.CloudantClientMgr.createClient(CloudantClientMgr.java:110)
[err]   at it.sinesy.nosql.CloudantClientMgr.initClient(CloudantClientMgr.java:45)
[err]   at it.sinesy.nosql.CloudantClientMgr.getDB(CloudantClientMgr.java:117)
[err]   at it.sinesy.nosql.ResourceServlet.getDB(ResourceServlet.java:307)
[err]   at it.sinesy.nosql.ResourceServlet.get(ResourceServlet.java:110)
[err]   at sun.reflect.GeneratedMethodAccessor751.invoke(Unknown Source)
[err]   at java.lang.reflect.Method.invoke(Method.java:498)
[err]   at com.ibm.ws.jaxrs20.server.LibertyJaxRsServerFactoryBean.performInvocation(LibertyJaxRsServerFactoryBean.java:646)
[err]   at [internal classes]
[err]   at javax.servlet.http.HttpServlet.service(HttpServlet.java:687)
[err]   at com.ibm.websphere.jaxrs.server.IBMRestServlet.service(IBMRestServlet.java:96)
[err]   at [internal classes]
[err]   at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149)
[err]   at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624)
[err]   at java.lang.Thread.run(Thread.java:748)
[err] Caused by: com.cloudant.client.org.lightcouch.CouchDbException: 503 Service Unavailable: service_unavailable: Service unavailable
[err]   at com.cloudant.client.org.lightcouch.CouchDbClient.execute(CouchDbClient.java:526)
[err]   at com.cloudant.client.org.lightcouch.CouchDbClient.executeToInputStream(CouchDbClient.java:574)
[err]   at com.cloudant.client.org.lightcouch.CouchDbClient.get(CouchDbClient.java:320)
[err]   at com.cloudant.client.org.lightcouch.CouchDbClient.serverVersion(CouchDbClient.java:240)
[err]   at com.cloudant.client.api.CloudantClient.serverVersion(CloudantClient.java:272)
[err]   at it.sinesy.nosql.CloudantClientMgr.createClient(CloudantClientMgr.java:103)
[err]   ... 45 more

I'm not behind a proxy.. What am I doing wrong?

2
  • How do you get the server / account configuration? Commented Jun 15, 2018 at 8:46
  • @data_henrik from cloudant console "generate api key" I get the account/password and the database name. All the rest is done by cloudant library "cloudant-client 2.7.0" Commented Jun 15, 2018 at 9:02

2 Answers 2

3

from cloudant console "generate api key" I get the account/password and the database name

If you are using an API key then the account name is not the same as the username. In your example they are both called using user

ClientBuilder.account(account_name) // needs to be the part before .cloudant.com .username(api_key_user) // needs to be the API key

Secondly, API keys are per database so don't have permission to do getAllDbs().

FWIW the .build() doesn't actually connect to Cloudant it just builds a CloudantClient object, no HTTP requests happen until needed.

Sign up to request clarification or add additional context in comments.

Comments

0

Solved. User and password MUST be created from Cloudant service -> Service Credntial -> New Credential.

I was using wrong credential created from the cloudant -> database -> permissions -> Generate API Key.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.