CORS error when trying to get access token in Javascript using Axios and/or Fetch

MxkertMxkert Member Posts: 2

Hello,

I am trying to get an access token using Javascript.

I have visited the authorization URL (https://cloud.lightspeedapp.com/oauth/authorize.php?response_type=code&client_id={CLIENT_ID}&scope=employee:all) to get my token.

Now I am trying to send a request to get my access token but I get a CORS error:

Access to fetch at 'https://api.lightspeedapp.com/oauth/access_token.php' from origin 'https://{DOMAIN.COM}' has been blocked by CORS policy: Response to preflight request doesn't pass access control check: No 'Access-Control-Allow-Origin' header is present on the requested resource. If an opaque response serves your needs, set the request's mode to 'no-cors' to fetch the resource with CORS disabled.


When using Postman everything is working fine, but when trying through Javascript, using fetch for example, I get the CORS error. My code is as follows:


    const requestOptions = {

      method: 'POST',

      headers: { 'Content-Type': 'application/json', "Access-Control-Allow-Origin": "*" },

      body: {

        "client_id": "MY_ID",

        "client_secret": "MY_SECRET",

        "grant_type": "authorization_code",

        "code": "MY_AUTH_CODE_FROM_URL"

      }

    };

    fetch('https://api.lightspeedapp.com/oauth/access_token.php', requestOptions)

        .then(response => console.log(response))


Does anyone know how I can solve this CORS error?


Thanks in advance,

Mike

6 comments

Sign In or Register to comment.