When I make an HTTP POST request to the Microsoft login in order to obtain an access token for use with the mail API, the request is successful but my code still goes to the error clause.
requestAccessToken(code: string)
{
console.log("Requesting access token");
if (code) {
var headers = new Headers();
headers.append("Content-Type", 'application/x-www-form-urlencoded');
headers.append('Accept', 'application/json');
var requestoptions = new RequestOptions({
headers: headers
});
var body = `grant_type=authorization_code&
redirect_uri=http://localhost:4200&
code=`+ code + `&
client_id=4e[................]5ab&
client_secret=CE[..............]BC`;
this.http.post("https://login.microsoftonline.com/common/oauth2/v2.0/token", body, requestoptions).map((res: Response) =>
{
console.log("Response received");
const data = res.json();
}).subscribe( (data) => {
console.log("The data is = " + data);
}, error => {
console.log("The error is = " + error)
});
}
The browser console displays the following error message: XMLHttpRequest cannot load . No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://localhost:4200' is therefore not allowed access. zone.js:2019 XHR failed loading: POST "". outlook.service.ts:96 The error is = Response with status: 0 for URL: null
Here's a screenshot for better visualization: https://i.sstatic.net/cHO0W.png
However, it seems that despite the error messages, my request is actually successful as indicated here: https://i.sstatic.net/YVgeR.png
So why does my code still go to the error clause instead of retrieving the access token?