Cross origin POST request not working for Outlook API

540 Views Asked by At

I'm using this part of the Outlook API. It says that you should be able to do a post request, however when I try I get the following error:

Failed to load https://login.microsoftonline.com/common/oauth2/v2.0/token: No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://localhost:3003' is therefore not allowed access. The response had HTTP status code 400.

How do I fix this though? I obviously don't have access to Outlook's servers, but surely they would let me do a post request considering that's what it says to do in the documentation!.

Here is my code by the way if that helps:

<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
<script>
  let url = new URL(window.location);
  let code = url.searchParams.get('code');
  let redirect = 'http%3A%2F%2Flocalhost%3A3003%2Fauth-process-token.html';
  let clientId = '<MY ID>';
  let clientSecret = '<MY KEY>';

  var req_string = 'grant_type=authorization_code&code=' + code + '&redirect_uri=' + redirect + '&client_id=' + clientId + '&client_secret=' + clientSecret;

  $.ajax({
    type: 'POST',
    url: 'https://login.microsoftonline.com/common/oauth2/v2.0/token',
    crossDomain: true,
    data: req_string,
    dataType: 'application/x-www-form-urlencoded',
    success: function (responseData, textStatus, jqXHR) {
      var value = responseData.someKey;
    },
    error: function (responseData, textStatus, errorThrown) {
      console.log('POST failed.', errorThrown);
    }
  });
</script>

EDIT: I fixed the "bad request" error, but it still gives me the other one.

1

There are 1 best solutions below

4
On

One workaround for this is by using cors.io

http://cors.io/?http://your_link

so it would be

http://cors.io/?https://login.microsoftonline.com/common/oauth2/v2.0/token