A
A
Alexey Belov2019-06-13 19:19:38
JavaScript
Alexey Belov, 2019-06-13 19:19:38

Why does the request give a 401?

For two hours now I can’t understand why the request to backblaze does not work, it always gives 401, if you do it through CURL as shown in the documentation, then everything works.
curl https://api.backblazeb2.com/b2api/v2/b2_authorize_... -u "APPLICATION_KEY_ID:APPLICATION_KEY"

addEventListener('fetch', event => {
  event.respondWith(handleRequest(event.request))
})

async function handleRequest(request) {
  // Get Image
  const imageURL = 'https://ichef.bbci.co.uk/images/ic/720x405/p0517py6.jpg'
  let options = { cf: { image: {} } }
  let image = await fetch(imageURL, options);

  // B2
  const b2_url = 'https://api.backblazeb2.com/b2api/v2/b2_authorize_account'
  const accountId = 'dcd73331748'
  const applicationKey = '0028adf0ffdc6df733333f5e11c27033c52fdd28c'
  var auth = 'Basic ' + accountId + ':' + applicationKey
  var base = auth.toString('base64');

  b2_token_request = new Request(
        b2_url,{
        method:'GET',
        headers: {
            'Authorization': base,
        },
      })
  const b2_response = await fetch(b2_token_request)
  console.log(b2_response)
  
  return image;
}

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Alexey Belov, 2019-06-13
@Alenorze

toString does not convert to base64, you need to use btoa for this

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question