我有一个使用axios
库处理请求的React应用程序。因此,按照接下来的文章:微软登录oauth2问题
How to login with username/password using OAuth2 and microsoft login and HTTP request
我可以在邮差执行的操作。
然后,我成立了爱可信库进行POST
const dataForBody = `${'grant_type=password&' +
'username='}${encodeURI(userName)}&` +
`password=${encodeURI(userPassword)}&` +
`client_id=${encodeURI(clientID)}&` +
`resource=${encodeURI('https://graph.microsoft.com')}&` +
`client_secret=${encodeURI(clientSecret)}`;
const messageHeaders = {
'Content-Type': 'application/x-www-form-urlencoded'
};
axios({
method: 'post',
url: 'https://login.microsoftonline.com/{tenant}/oauth2/token',
headers: messageHeaders,
data: dataForBody,
})
.then((response) => {
});
,但我得到了以下错误:
Cross-Origin Request Blocked: The Same Origin Policy disallows reading the remote resource at https://login.microsoftonline.com/ {tenant}/oauth2/token. (Reason: CORS header ‘Access-Control-Allow-Origin’ missing).
我尝试添加:
'Access-Control-Allow-Origin': 'https://login.microsoftonline.com',
的头,但它没有奏效。
因此,添加Allow-Control-Allow-Origin: *
铬扩展修复了我的问题。
事情是,我的应用程序将在azure上发布,因此我在其他Web浏览器上测试了该请求,但它无效。所以我不希望我的用户安装扩展。
我的要求有问题吗?为什么邮差可以做到这一点,而无需设置标题?
是否有其他方法来实现它?
PS:我读了关于使用adal.js
,但我不想使用微软的登录屏幕,因为我知道用户和通过的应用程序,我想避免手动登录。