Data Sync problem - got 401 login required error

#1

Hi all, I got the below error message when data sync is performed. My code is shown as below:

Error message in sync gateway:


POST /db/changes
—> 401 Login required <1.0 ms>


public void startSync() {

    URL syncUrl;
    try {
        syncUrl = new URL(SYNC_URL);
    } catch (MalformedURLException e) {
        throw new RuntimeException(e);
    }

    Replication pullReplication = database.createPullReplication(syncUrl);
    pullReplication.setContinuous(true);

    Replication pushReplication = database.createPushReplication(syncUrl);
    pushReplication.setContinuous(true);

    Authenticator auth = AuthenticatorFactory.createBasicAuthenticator("adminaccount", "password");
    pullReplication.setAuthenticator(auth);
    pushReplication.setAuthenticator(auth);
            
    pullReplication.start();
    pushReplication.start();


}  

What’s wrong of my code? Many thanks for help.

#2

Did you create the user (adminaccount, password) through the Sync Gateway Admin REST API?

curl -vX POST http://localhost:4985/todos/_user/
       -H 'Content-Type: application/json'
       -d '{"name": "adminaccount", "password": "password"}'

James

#3

I got the similar problem for my data sync if using admin account login on sync Gateway with 1.1.0 version.

Error Log
10:48:22,079 SEVERE [com.couchbase.lite] (CBLRequestWorker-38c6f-0) Sync: RemoteRequest.run() exception: %s
java.lang.NoClassDefFoundError: org/apache/commons/codec/binary/Base64
at org.apache.http.impl.auth.BasicScheme.authenticate(BasicScheme.java:170)
at org.apache.http.impl.auth.BasicScheme.authenticate(BasicScheme.java:142)
at org.apache.http.client.protocol.RequestTargetAuthentication.process(RequestTargetAuthentication.java:101)
at org.apache.http.protocol.BasicHttpProcessor.process(BasicHttpProcessor.java:290)
at org.apache.http.protocol.HttpRequestExecutor.preProcess(HttpRequestExecutor.java:160)
at org.apache.http.impl.client.DefaultRequestDirector.execute(DefaultRequestDirector.java:391)
at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:555)
at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:487)
at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:465)
at com.couchbase.lite.support.RemoteRequest.executeRequest(RemoteRequest.java:184)
at com.couchbase.lite.support.RemoteRequest.run(RemoteRequest.java:103)
at java.util.concurrent.Executors$RunnableAdapter.call(Unknown Source)
at java.util.concurrent.FutureTask.run(Unknown Source)
at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.access$201(Unknown Source)
at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.run(Unknown Source)
at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
at java.lang.Thread.run(Unknown Source)
Caused by: java.lang.ClassNotFoundException: org.apache.commons.codec.binary.Base64 from [Module “deployment.Web.war:main” from Service Module Loader]
at org.jboss.modules.ModuleClassLoader.findClass(ModuleClassLoader.java:213)
at org.jboss.modules.ConcurrentClassLoader.performLoadClassUnchecked(ConcurrentClassLoader.java:459)
at org.jboss.modules.ConcurrentClassLoader.performLoadClassChecked(ConcurrentClassLoader.java:408)
at org.jboss.modules.ConcurrentClassLoader.performLoadClass(ConcurrentClassLoader.java:389)
at org.jboss.modules.ConcurrentClassLoader.loadClass(ConcurrentClassLoader.java:134)

Below is my testing code

        Manager m = new Manager(new JavaContext(), Manager.DEFAULT_OPTIONS);
        Database db = m.getDatabase("test");
               
        URL url = new URL("http://192.168.222.238:4984/test");
        Replication pull = db.createPullReplication(url);
        pull.setContinuous(true);
        Authenticator auth = AuthenticatorFactory.createBasicAuthenticator("admin","123"); 
        pull.setAuthenticator(auth);
        
        pull.start();

Please help, thanks.

#4

Does the user with name admin and password 123 exist in Sync Gateway?
See this tutorial for more info on creating users through the admin rest api.

James

#5

I 've inputted the following in the browser
http://192.168.222.238:4985/test/_user/admin

And it return the following info.

{“name”:“admin”,“admin_channels”:[""],“all_channels”:["!",""],“admin_roles”:[“admin”],“roles”:[“admin”]}

#6

What are you trying to do?

4985 is the admin port so authentication will always be successful if the user exists in Sync Gateway.

#8

Continuing the discussion from Data Sync problem - got 401 login required error:

Is it not your expected result? this is not correct way to check the user exists or not? please advice the method for checking…

#9

Yes you can use this endpoint to check if the user exists.
But I’m not sure what isn’t working?
Are you still getting the 401 Unauthorised error when replicating with the Android SDK?

#10

Hi @saitat,

From the stacktrace you provided, ClassLoader could not load the org.apache.commons.codec.binary.Base64.
I assume this could be caused by org.jboss.modules.ConcurrentClassLoader that I am not familiar with.

Is it possible to avoid to use org.jboss.modules.ConcurrentClassLoader on your app?
Or you can add https://commons.apache.org/proper/commons-codec/ as a dependency?

Hope this information helps you.

Thanks,
Hideki