-2

Trying to acces an external API through my react.js application to get a Bearer token. But i'm struggling with the following error:

Access to fetch at 'https://login.bol.com/token?grant_type=client_credentials' from origin 'http://localhost:3000' has been blocked by CORS policy: Response to preflight request doesn't pass access control check: It does not have HTTP ok status.

Therefore I'm using the following code:

let url = 'http://login.bol.com/token?grant_type=client_credentials';

let client_id = 'client_id';
let client_secret = 'secret_key';
var basicAuth = 'Basic ' + btoa(client_id + ':' + client_secret);

function fetchToken() {
    return fetch(url, {
        method: 'POST',
        headers: {
            'Content-Type': 'application/json',
            'Authorization': basicAuth
        },
    })
        .then(data => data.json())
}

fetchToken();

All though, when I use postman it works:

enter image description here

Kahoot
  • 15
  • 1
  • Read about [CORS](https://developer.mozilla.org/pt-BR/docs/Web/HTTP/CORS) Basically you can't request this API from a browser unless the API allows requests from your origin domain (in your case, localhost) – Otacílio Maia May 24 '22 at 14:40
  • Does this answer your question? [Why does my JavaScript code receive a "No 'Access-Control-Allow-Origin' header is present on the requested resource" error, while Postman does not?](https://stackoverflow.com/questions/20035101/why-does-my-javascript-code-receive-a-no-access-control-allow-origin-header-i) – jabaa May 24 '22 at 14:57

0 Answers0