【发布时间】:2018-02-09 02:05:45
【问题描述】:
我正在尝试获取访问令牌以访问word online add-in 内的用户的一个驱动器。我正在按照this link 上的说明进行操作。
我使用以下 URL 从我正在开发的 ms word 在线插件中请求授权代码。
https://login.microsoftonline.com/common/oauth2/v2.0/authorize?
client_id=6731de76-14a6-49ae-97bc-6eba6914391e
&response_type=code
&redirect_uri=http://localhost/myapp/
&response_mode=query
&scope=offline_access user.read mail.read
&state=12345
我使用以下代码生成 GET 请求:
function getAuthorizationCode() {
var xhr = new XMLHttpRequest();
xhr.open(
"GET",
"https://login.microsoftonline.com/common/oauth2/v2.0/authorize?\
client_id=6731de76-14a6-49ae-97bc-6eba6914391e\
&response_type=code\
&response_mode=query\
&scope=offline_access%20user.read%20mail.read\
&state=12345",
true
);
xhr.send();
xhr.addEventListener("readystatechange", processRequest, false);
xhr.onreadystatechange = processRequest;
function processRequest(e) {
if (xhr.readyState == 4 && xhr.status == 200) {
var response = JSON.parse(xhr.responseText);
}
}
}
但是服务器抛出错误说
No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'https://localhost:3000' is therefore not allowed access.
如果上面的链接被复制并粘贴到浏览器中,它就可以正常工作。 我应该怎么办?有没有其他方法可以在 MS Word Online 加载项中获取访问令牌。
【问题讨论】:
-
欢迎来到 Stack Overflow!由于您是新来的,我建议您阅读"How do I ask a good question?" 以获取一些提示。您的问题缺乏足够的细节来帮助社区帮助您。
-
生成的代码缺少 redirect_uri,其中令牌授权端点使用代码重定向流。
-
@Max 添加
&redirect_uri=http%3A%2F%2Flocalhost%3A3000参数后还是不行 -
尝试使用 microsoft javascript 库 MSAL.js 和 SPA example。您可以通过link了解更多信息。
-
您正在构建什么样的 Word 加载项?它是 Web 插件 (
office.js) 吗?
标签: oauth-2.0 ms-word microsoft-graph-api