【问题标题】:MIcrosoft Graph get token request failing due to CORS由于 CORS,Microsoft Graph 获取令牌请求失败
【发布时间】: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.jsSPA example。您可以通过link了解更多信息。
  • 您正在构建什么样的 Word 加载项?它是 Web 插件 (office.js) 吗?

标签: oauth-2.0 ms-word microsoft-graph-api


【解决方案1】:

Office Web 插件支持基于应用程序用户提取令牌。这提供了 SSO 体验,并且无需让他们使用他们已经登录到应用程序的相同凭据重新进行身份验证。

您可以在Enable single sign-on for Office Add-ins 阅读有关此功能的信息。

【讨论】:

  • 我已经在 2 天前尝试过,但它也无法正常工作。调用getAccessTokenAsync 会引发错误。请参阅this 线程。
猜你喜欢
  • 2019-11-02
  • 1970-01-01
  • 2011-08-01
  • 1970-01-01
  • 1970-01-01
  • 2023-03-09
  • 2019-05-15
  • 2023-01-01
  • 2016-10-18
相关资源
最近更新 更多