2016-11-11 61 views
1

I请求OAuth2服务器的authorization code。 我的目的是授权用户与我的微软应用程序。 Refered DocumentOAuth2访问原始错误

我对GET呼叫尝试:

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(); 
    } 

但是这给下面的错误:

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

然后我加入req.setRequestHeader("Access-Control-Allow-Origin", "*");

,但它提供了以下错误:

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

+0

你需要在服务器上配置这一点,而不是角应用 – jonrsharpe

+0

@jonrsharpe什么你的意思“做你需要在服务器上配置它“? –

+0

我的意思是你提出请求的服务器。尽管这似乎超出了你的控制范围。 – jonrsharpe

回答

0

不使用我想出了解决方案的任何frontend谷歌库。

window.open("url") 

完成认证后,我得到了codeurl params并将其发送backend和实现access token, refersh token.......etc,

1

要在JavaScript中集成AAD,我们建议您使用azure-activedirectory-library-for-js这是一个JavaScript前端的库,用于轻松集成AAD。

有2个选项,我们需要注意的,我们使用ADAL的JS面前:

下面是代码示例从微软收购图形访问令牌:

<script src="https://secure.aadcdn.microsoftonline-p.com/lib/1.0.10/js/adal.min.js"></script> 

<body> 
<a href="#" onclick="login();">login</a> 
<a href="#" onclick="getToken()">access token</a> 
</body> 
<script type="text/javascript"> 
    var configOptions = { 
     tenant: "<tenant_id>", // Optional by default, it sends common 
     clientId: "<client_id>", 
     postLogoutRedirectUri: window.location.origin, 
    } 
    window.authContext = new AuthenticationContext(configOptions); 

    var isCallback = authContext.isCallback(window.location.hash); 
    authContext.handleWindowCallback(); 

    function getToken(){ 
     authContext.acquireToken("https://graph.microsoft.com",function(error, token){ 
      console.log(error); 
      console.log(token); 
     }) 
    } 
    function login(){ 
     authContext.login(); 
    } 
</script>