401 error with cordova android


#1

Hi,

I’ve been developing my app with cordova on ios and it’s working without issue. Now that I’ve tried to run a build on the android platform, I’m getting the following error:

http://74029a44-9d2f-4a90-a2ba-c9e68513a3f7:3a7e7b34-cbe8-4159-9b0f-dcb8f7a7e962@localhost:5984/my_app Failed to load resource: the server responded with a status of 401 (Unauthorized)

Can anyone assist with this problem?


Getting 401 response when using Android 4.x
#2

Looks like I wasn’t the only one who experienced this issue, Getting 401 response when using Android 4.x.

I’m in a little time crunch with trying to get the android released at the same time as ios. Please help.


#3

Hi @agillette,
How do you authenticate against Sync Gateway? do you set sync gateway session id in cookie?


#4

Hi @hideki,

The error is occurring during my application initialization. It looks like the app is getting the 401 when it’s trying to connect to device cblite. This is before registration and attempting to auth against sync gateway.

This issue is only happening on Android.


#5

One other bit of info that may be helpful is that I’m using cordova-android Version 5.1.1, cordova-ios Version 4.1.0.


#6

@agillette,
Could you please capture log messages from logcat?


#7

I don’t work on Android, but what I know about this is: The CBL HTTP listener on Android has to open an actual TCP port on the device. To prevent other apps, not trusted by you, from accessing your databases, the listener is initialized with a randomly generated username/password. This gets inserted into the database URL given to you (the two hex blobs before the @.)

No idea why this wouldn’t be working. How are you sending HTTP requests? Maybe the library you’re using is accidentally stripping out or mangling the username/password?


#8

Hi @hideki, I’ll run a test later today and post the log of interest.

@jens, this is the only statement my app has to connect to the device db:

`
// 1st connect to couchbase that’s located
// on the device and get it’s url
cblite.getURL(function (error, url) {
if (error) {
console.log('CB Initilization error ’ + JSON.stringify(error));
reject(‘CB Initilization error’, error);
}

    couchUrl = url;
    resolve('CB connected');
  });`

#9

I’ve encountered the same issue on both the Android emulator and a physical device.


#10

@hideki,

What specifically in the logcat should I be looking for?


#11

Quick update,

So cblite.getURL is returning “http://5f4b99a1-9a21-4500-a17f-f06be609b905:2348eac3-d57f-4d94-87bb-1b5b027ab8eb@localhost:5984/

Then I attempt to do a GET request to see if the database exists, in which CB responds with a 401. I’m expecting a 404.


#12

@jens,

I’m using a standard fetch GET call. It’s working with no issues on IOS.


#13

Hi @agillette,

404 usually indicates authentication error. So userid or password could be wrong. So I would like to know why authenticate fails. Log messages from CBL Java library could help to debug.

Have you ever tried ToDo Lite PhoneGap on your device? If not, can you try it if it works?

Thanks!


#14

@hideki,

Thanks for your suggestion. I’ll let you know how that goes.

BTW -I’m not getting 404 error, I’m getting a 401 error.


#15

How exactly are you sending the GET request that fails?


#16

@jens, I’m using this library, https://github.com/github/fetch.

In their “caveat” section, it states the following:

By default, fetch won’t send any cookies to the server, resulting in unauthenticated requests if the site relies on maintaining a user session.

Could this be the issue?


#17

Cookies aren’t an issue. However, if fetch ends up stripping the username/password out of the URL (or inadvertently altering them?), then it would cause these results.

(You wouldn’t have noticed on iOS, because on that platform we don’t use a password for REST-API access to CBL because we don’t open a real TCP port.)


#18

My fetch statement is like this:

fetch(‘http://5f4b99a1-9a21-4500-a17f-f06be609b905:2348eac3-d57f-4d94-87bb-1b5b027ab8eb@localhost:5984/my-app’);


#19

I just ran the todo-list app and here’s the cblite url:

http://b7524a5f-8a40-4554-b4b0-23f788a33216:be894f68-7013-412c-b2e1-bc7768d66abc@localhost:5985/

I notice that the port for the todo-list is 5985 vs my app, which is 5984.

Not sure why cblite.getURL() is returning different port numbers for these two apps.


#20

You’ll need to look into the implementation of fetch to see whether it alters the URL before it calls the actual XHR.

The port can end up different if the default port number (5984) is in use already.