3

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

Maciej Treder
  • 11,866
  • 5
  • 51
  • 74
Wouter Vandenputte
  • 1,948
  • 4
  • 26
  • 50
  • If you get this answer, can't you just take the `access_token` or the `refresh_token` out of it ? –  May 24 '17 at 09:48
  • No, the subscribe doesn't go to the first part but to the second part which is the error part. as you can see, the log only logs the error – Wouter Vandenputte May 24 '17 at 10:30
  • I'm sorry, corporate proxy, imgur is filed under 'Image board' website and I don't see the error. But if this happens, it means that your app isn't registered on the server side. Can't you set it on a dashboard or something ? –  May 24 '17 at 10:35
  • In RequestOptions set header accept type to be json headers.append('Accept', 'application/json'); and check the response header access control allow origin pattern.. something like Access-Control-Allow-Origin:* – Karthigeyan Vellasamy May 24 '17 at 11:46

0 Answers0