OAuth2访问源错误 [英] OAuth2 Access origin error

查看:64
本文介绍了OAuth2访问源错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我从 OAuth2 服务器请求一个授权码.我的目的是通过我的Microsoft App授权用户.引用文档

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

我尝试拨打电话:

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

但这会产生以下错误:

请求的请求上没有'Access-Control-Allow-Origin'标头资源.

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

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

但出现以下错误:

对预检请求的响应未通过访问控制检查:否请求中存在"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.

推荐答案

我不使用任何前端谷歌库,但我想出了解决方案.

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

window.open("url") 

完成身份验证后,我从 url参数中获取了 code 并将其发送给 backend ,并获得了 access令牌,referh令牌.......等,

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

这篇关于OAuth2访问源错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆