Cloudant from Cloudflare workers giving authentication error

195 Views Asked by At

I am using Cloudant as my DB and am looking to use Cloudflare workers as the public-facing interface.

Unfortunately, I've been having a number of errors authenticating to the database using libraries like PouchDB. Instead, I'm trying to get the most basic connection possible and build up from something working.

From a basic curl command on my computer, I can query my view. Attempting to do the same thing from a Worker is giving me an "unauthorized" error.

The specific code (with the username and password obviously masked)

(() => {
  // src/index.js
  async function handleRequest() {
    let results = await fetch("https://<key>:<pass>@<uid>-bluemix.cloudantnosqldb.appdomain.cloud/mnb/_design/score/_view/score?reduce=true&group=true");
    results = await results.json();
    let str = JSON.stringify(results);
    return new Response(str, init);
  }
  addEventListener("fetch", (event) => {
    return event.respondWith(handleRequest());
  });
})();
//# sourceMappingURL=index.js.map

The error I'm getting.

{
  "error": "unauthorized",
  "reason": "one of _view, _design, _reader is required for this request"
}

Is there something I'm not getting with the fetch call?

UPDATE

To confirm, the curl command I used was dead simple

curl https://<key>:<pass>@<uid>-bluemix.cloudantnosqldb.appdomain.cloud/mnb/_design/score/_view/score?reduce=true\&group=true

I cut paste from the worker into the terminal to be sure I didn't have a typo

1

There are 1 best solutions below

1
zmf On

Try to pass your credentials using the Authorization header in your request.

The following example targets a page protected using Basic Authentication with credentials user / pass.

  const b = await fetch('https://authenticationtest.com/HTTPAuth/', { 
    headers: {
      'Authorization': `Basic ${btoa('user:pass')}`
    }
  })