Januka samaranyake Januka samaranyake - 15 days ago 6
Javascript Question

OAuth2 Access origin error

I Request an

authorization code
from
OAuth2
Server.
My purpose is to authorize user with my microsoft App.
Refered Document

My attempt for get Call:

function httpGet(){
var theUrl = "https://login.microsoftonline.com/common/oauth2/v2.0/authorize?client_id="client_id"&response_type=code&redirect_uri="redirect_uri"&response_mode=query&resource=https%3A%2F%2Fservice.contoso.com%2F&state=12345";

var req = new XMLHttpRequest();
req.open('GET', theUrl, true);
req.onreadystatechange = function() {
if (req.readyState === 4) {
if (req.status >= 200 && req.status < 400) {
console.log(req.responseText)
} else {
console.log("error")
}
}
};
req.send();
}


but this gives below error:


No 'Access-Control-Allow-Origin' header is present on the requested
resource.


then I add the
req.setRequestHeader("Access-Control-Allow-Origin", "*");


but it gives the below error:


Response to preflight request doesn't pass access control check: No
'Access-Control-Allow-Origin' header is present on the requested
resource.

Answer

Without using any frontend google libraries I came up with solution.

window.open("url") 

After complete the authentication I get the code from url params and send it backend and achieve the access token, refersh token.......etc,