CORS outlook api : not allowed access

Viewed 469

I cannot count how many times I sweared on CORS. Right now we are trying to access the outlook API to send emails and stuff. We follow the tutorial, do everything on Postman and that works. Now we want to implement it in our Angular 2 application with the following code:

requestAccessToken(code: string) 
{
  if (code) {
  var headers = new Headers();
  headers.append("Content-Type", 'application/x-www-form-urlencoded');
  var requestoptions = new RequestOptions({
    headers: headers,
    withCredentials: false // tried true too
  })
  let body = `grant_type=authorization_code&
            redirect_uri=http://localhost:4200&
            code=`+ code + `&
            client_id=4e...ab&
            client_secret=CE.....BC`

  this.http.post("https://login.microsoftonline.com/common/oauth2/v2.0/token", body, requestoptions).subscribe((data) =>
  {
    console.log("data: " + data);
  },
    error =>
    {
      console.log("error: " + error);
    });
  }
}

Our response looks like this:

{
"token_type":"Bearer",
"scope":"calendars.read calendars.read.shared calendars.readwrite calendars.readwrite.shared contacts.read 
contacts.read.shared mail.read 
user.read",
"expires_in":3599,"ext_expires_in":0,
"access_token":"ey...NjQ",
"refresh_token":"OAQABAAA...Fd8JA"
}

Which is exactly but I want, but however I cannot extract the token out of it and my browser logs the following:

Google Chrome log

As you can see, the error is logged and not the data and Chrome complains about CORS. I'm really stuck and the only thing the internet says is to change server settings, which is of course not possible with the URL login.microsoftonline.com

0 Answers
Related