经过我所有的研究和阅读 Microsoft 文档后,我无法得出结论如何使用 Outlook Web 插件中的身份验证代码流生成访问令牌。在 Dialog API 的帮助下,我可以使用隐式授权流成功生成访问令牌,它只是打开 URL,我们从地址栏中读取访问令牌。
为了更安全,我正在尝试使用身份验证代码流在 azure 中使用注册的应用程序生成图形 API/(自定义 SharePoint 网站)的访问令牌。
Azure 应用设置:在 Microsoft Graph 下添加了 API 权限。[User.Read, Sites.Read.All] 重定向 Web URI:myapp/Auth_Result 重定向 SPA URI:myapp/Auth_Result_SPA
Outlook Web 加载项中的代码:
使用 Dialog API 我打开重定向到的 myapp/Auth 页面
https://login.microsoftonline.com/<Tenant ID>/oauth2/v2.0/authorize
?client_id=<Client ID>
&response_type=code
&redirect_uri=https%3A%2F%2Flocalhost%3A44333%2FAuth_result.html
&response_mode=query&scope=https%3A%2F%2Fgraph.microsoft.com%2Fuser.read
&state=12345
上面的 URL 在读取的重定向页面 myapp/Auth_result 中生成授权码。
Auth_Result 页面中的代码
function GetAccessToken(authCode) {
try {
var url = 'https://login.microsoftonline.com/<Tenant ID>/oauth2/v2.0/token';
var params = 'client_id=<Client ID>&scope=https%3A%2F%2Fgraph.microsoft.com%2Fuser.read&code=' + authCode + '&redirect_uri=https://localhost:44333/Auth_Result_SPA.html&grant_type=authorization_code';
var xhr = new XMLHttpRequest();
xhr.open('POST', url);
xhr.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
xhr.onload = function (result) {
AccessTokenCallback(result);
}
xhr.send(params);
}
catch{ }
}
上述请求失败
Access to XMLHttpRequest at 'https://login.microsoftonline.com/<Tenant ID>/oauth2/v2.0/token' from origin 'https://localhost:44333' has been blocked by CORS policy: No 'Access-Control-Allow-Origin' header is present on the requested resource.
MessageRead.js:780 POST https://login.microsoftonline.com/<Tenant ID>/oauth2/v2.0/token net::ERR_FAILED
清单中的应用程序域
<AppDomain>https://login.microsoftonline.com/<Tenant ID>/oauth2/v2.0/token</AppDomain>
<AppDomain>https://login.microsoftonline.com</AppDomain>
<AppDomain>https://localhost:44333</AppDomain>
<AppDomain>https://localhost:44333/Auth</AppDomain>
<AppDomain>https://localhost:44333/Auth_Result</AppDomain>
<AppDomain>https://localhost:44333/Auth_Result_SPA</AppDomain>
<AppDomain>https://graph.microsoft.com</AppDomain>
我不确定我错过了什么?现在我质疑是否可以在 Outlook Web 插件中使用身份验证代码流?
伙计们,任何建议都会有所帮助。
谢谢
------编辑-------------
我尝试使用重定向 URI 作为 Web 平台的上述解决方案。
如果我在 azure app Authentication 中使用 SPA 平台重定向 URI,则会收到以下错误:
由于某种原因,图像没有出现。错误文本:
AADSTS9002325:跨域授权代码兑换需要代码交换证明密钥。
[![enter image description here][1]][1]